//本人的不产生四位重复四位数的代码,你可以直接复制后就可以用了,应没有什么问题的。我测试过。
求一个四位数不出现重复数字
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
bool IsRepeat(int x)//用来判断四位数是否有重复数字
{
int a,b,c,d;//分别用来标识个,十,百,千位。
d=x/1000;
x=x%1000;
c=x/100;
x=x%100;
b=x/10;
a=x%10;
if(a!=b&&b!=c&&c!=d&&a!=d&&a!=c&&b!=d)
return true;
return false;
}
void main( void )
{
int i;//用来产生四位数;
int flag=1;//用来标识是否找到这样的四位;
srand( (unsigned)time( NULL ) );
while(flag)
{
i=rand();
if(i>1000&&i<9999&&IsRepeat(i))
{
printf("%d\n",i);
flag=0;
}
}
}
评论