童话先生电影:C语言编写猜数字问题

来源:百度文库 编辑:中科新闻网 时间:2024/05/07 12:01:44
要求随机产生5个数(0-9)。在被printf前判断产生的5个数中是否有重复。有重复再产生另个不重复。如果重复提示重复。而且要显示重复再产生的次数。哪为高人指点下重复再产生另个数及提示重复和显示再产生的代码

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main(){
int a[5];
int i,total=0;
int flag=0;
srand((unsigned)time(0));
while (flag==0)
{
for (i=0; i<5; i++)
{
a[i]=rand() % 10;
}
if ((a[0]==a[1]) || (a[1]==a[2]) || (a[2]==a[3]) || (a[3]==a[4]) || (a[0]==a[2]) || (a[1]==a[3]) || (a[2]==a[4]) || (a[0]==a[3]) || (a[1]==a[4]) || (a[0]==a[4]))
{
total++;
printf("重复第%d次\n",total);
}
else
{
flag=-1;
}
}
//程序运行到这里的时候a数组内存的就是5个不重复的数字
}