C语言的考试题.........

来源:百度知道 编辑:UC知道 时间:2024/06/21 15:04:16
让电脑随机生成1个4位数,让人去踩,猜的次数无限,猜对则显示*,猜错位置显示#,这个怎么写....
604790441

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

void main(void)
{
int i, rand_num;
int a,b,c,d,e,f,g,h;
char m[5] ="????";
srand((unsigned)time(NULL));

do
{
rand_num = rand()%9000+1000;
a = rand_num/1000;
b = (rand_num-a*1000)/100;
c = (rand_num-a*1000-b*100)/10;
d = (rand_num-a*1000-b*100-c*10)/1;

//printf("%d,%d,%d,%d,%d\n",rand_num,a,b,c,d);

if(a==b||a==c||a==d||b==c||b==d||c==d)continue;
break;

}while(1);

//printf("%d\n",rand_num);

do
{
printf("请输入一个4位数(如果输入0则退出该程序):");
scanf("%d", &i);
if( i == 0)
{
printf("正确答案为: %d\n",rand_num);
break;
}
printf("输入的数字为%d:\n",i);