?? 電腦出題人來做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 + -