📄 电脑出题人来做c语言模拟.txt
字号:
电脑出题人来做C语言模拟[原创]
程序运行效果:计算机随即产生一个四位数,然后让人来猜,并且每次提示猜对的位数,直到用户猜对为止。如某次运行结果如下,计算机随即产生5130,我猜的过程如下:
Guess what number I have thought of:4567
I am sorry.You are wrong.Only 0 digits are right.Try another one.1234
I am sorry.You are wrong.Only 1 digits are right.Try another one.1235
I am sorry.You are wrong.Only 1 digits are right.Try another one.1432
I am sorry.You are wrong.Only 1 digits are right.Try another one.1345
I am sorry.You are wrong.Only 0 digits are right.Try another one.1234
I am sorry.You are wrong.Only 1 digits are right.Try another one.2431
I am sorry.You are wrong.Only 1 digits are right.Try another one.2234
I am sorry.You are wrong.Only 1 digits are right.Try another one.3234
I am sorry.You are wrong.Only 1 digits are right.Try another one.4231
I am sorry.You are wrong.Only 1 digits are right.Try another one.5231
I am sorry.You are wrong.Only 2 digits are right.Try another one.5131
I am sorry.You are wrong.Only 3 digits are right.Try another one.5132
I am sorry.You are wrong.Only 3 digits are right.Try another one.5133
I am sorry.You are wrong.Only 3 digits are right.Try another one.5134
I am sorry.You are wrong.Only 3 digits are right.Try another one.5135
I am sorry.You are wrong.Only 3 digits are right.Try another one.5136
I am sorry.You are wrong.Only 3 digits are right.Try another one.5137
I am sorry.You are wrong.Only 3 digits are right.Try another one.5138
I am sorry.You are wrong.Only 3 digits are right.Try another one.5139
I am sorry.You are wrong.Only 3 digits are right.Try another one.5130
Great!You are right.The number I have thought of is:5130
好了,代码是这样的:
/*
程序功能:计算机随机产生一个数,人来猜,计算机提示猜对的位数
作者:BugEyes
主页:http://bugeyes.blog.edu.cn
*/
#i nclude <stdlib.h>
#i nclude <time.h>
void main()
{
int pc,pc_array[4];
int user,user_array[4];
int i,count;
int t;
srand(time(NULL));/*种子*/
pc=(random(9)+1)*1000+random(1000);/*产生随机数*/
t=pc;
for(i=0;i<4;i++)/*把随机数放入数组以便判断*/
{
pc_array[i]=t%10;
t=t/10;
}
printf("\nGuess what number I have thought of:");
scanf("%d",&user);
while(user<1000||user>9999)/*保证用户猜的数为4位*/
{
printf("\nPlease input a 4-digit number:");
scanf("%d",&user);
}
for(i=0;i<4;i++)/*把用户猜的4位数放入数组以便判断*/
{
user_array[i]=user%10;
user=user/10;
}
do
{
count=0;/*恢复*/
for(i=0;i<4;i++)
if(pc_array[i]==user_array[i])
count++;/*猜对的位数*/
if(count==4)/*猜对了*/
{
printf("\nGreat!You are right.The number I have thought of is:%d",pc);
return;/*结束程序*/
}
printf("\nI am sorry.You are wrong.Only %d digits are right.Try another one.",count);/*提示用户猜对的位数*/
scanf("%d",&user);
while(user<1000||user>9999)
{
printf("\nPlease input a 4-digit number:");
scanf("%d",&user);
}
for(i=0;i<4;i++)
{
user_array[i]=user%10;
user=user/10;
}
}while(count<4);/*用户没有猜对,继续猜*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -