//c语言经典题目百钱买百鸡新思路:随机数筛查结果,时间复杂度O(n),比穷举算法更优,代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define max 100008
int judge_exist(int array[],int n){//筛选非重复值
int i;
for(i=0;i<100000;i++){
if(array[i]==n){
return 0;
}
}
return 1;
}
void main(){
int times,history[max],i;
srand((unsigned)time(NULL));//真正随机化
for(i=0;i<=100007;i++)
history[i]=-1;
for(times=0;times<max;times++){
int a,b,c;
a = rand()%100;
b = rand()%100;
c = 100-a-b;
if(c>=0&&c%3==0&&5*a+3*b+c/3==100){//判断百钱买百鸡条件
if(judge_exist(history,a)){
printf("%d,%d,%d\n",a,b,c);
history[times]=a;//存入历史记录
}
}
}
}
【lmh】RAND鸡
最新推荐文章于 2024-06-28 15:09:16 发布