如何用函数生成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]);
}
}