首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

怎么用函数生成4个不重复的数字

2012-02-14 
如何用函数生成4个不重复的数字?我是新手我想请问如何用函数实现生成4个不重复的数字(0-9之间的数字).并存

如何用函数生成4个不重复的数字?
我是新手
我想请问如何用函数实现生成4个不重复的数字(0-9之间的数字).
并存入整形数组中.
实在没想出来
在线等ing.......

[解决办法]
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>

bool contains(int* array, int n, int length) {
int i = 0;
for (i = 0; i < length; i++) {
if (array[i] != n) {
return false;
}
}

return true;
}

void main(){
int result[4] = {-1, -1, -1, -1};
int index = 0;
int temp = 0;

srand(time(0));
for (index = 0; index < 4;) {
temp = rand() % 10;
if (!contains(result, temp, 4)) {
result[index++] = temp;
}
}

for (index = 0; index < 4; index++) {
printf( "%d\t ", result[index]);
}
}


热点排行