作业帮 > 综合 > 作业

C语言程序:从N个数中随机取出100个不同的数

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/22 03:20:28
C语言程序:从N个数中随机取出100个不同的数
/**你题目中的N个数至少得大于100吧.下面的程序N个数是随机生成
你的N个数是?同时这个程序有错误的话请告诉我.
*/
/*
*从N个数中随机取出100个不同的数
*@author:banxi1988
*/
#include
#include
#include
int main(void){
int numbers[4096];
int nums[100];
int num;
int count=0;
int i = 0;
int j = 0;
int flag = 0;
int index= 0;
srand((unsigned)time(NULL));
/**随机生成0到8192的数放到数组numbers当中*/
for(i = 0; i < 4096; i++){
numbers[i] = rand()%8192;
}//for:
/**从numbers数组当中的数中随机取100个不同的数*/
for(i = 0; i< 100;i++){
flag = 1;
do{
index = rand()%4096;
num = numbers[index];
for(j = 0; j < count; j++){
if(num==nums[j]){
flag = 0;
break;
}//:if
}//:for
}while(flag==0);
nums[count++]=num;
}///for:
printf("从0到8192中产生的100个随机的不相同的数如下:\n");
for(i = 0; i< 100;i++){
if(i%10==0)putchar('\n');
printf("%5d",nums[i]);
}
putchar('\n');
return 0;
/*运行结果如下:
从0到8192中产生的100个随机的不相同的数如下:
4206 7853 1327 4541 398 754 5374 5259 258 4411
2069 4161 5186 5419 1746 1531 1957 7334 157 1693
5140 6087 1879 6489 2659 1210 102 2302 1522 7118
603 1711 1322 4489 6386 2732 3418 2203 4000 1309
4269 4382 2389 7617 5899 5054 818 4337 1247 5513
7369 2756 2956 7529 6623 6744 5229 4684 7251 2584
5868 3451 1570 4214 1106 1487 4682 5848 4730 3291
6084 7570 602 2128 2447 1741 714 7384 5965 2182
439 5286 2605 6820 6221 6646 1348 3899 3016 4971
2381 2410 1126 1984 1633 2395 5670 1890 5346 2713
**/
}//:main