c语言随机生成n个数求最小值,C语言程序:从N个数中随机取出100个不同的数

该C语言程序用于从4096个随机生成的0到8192的数中选取100个不重复的数,并打印这些数。程序使用do-while循环和数组来确保选取的数是唯一的。最后,可以在此基础上添加代码找出这100个数的最小值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**你题目中的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值