昊 天 翱 龙 » 日志 » 编写了一个猜数字的小东西
编写了一个猜数字的小东西
昊天翱龙 发表于 2006-05-01 13:07:11
C语言程序设计的习题里的东西,就是文曲星里的那个猜数字游戏,觉得蛮有意思,就编了一下。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main()
{
int a[4], b[4]; int i, j, k, n, right, match; long now;
srand(time(&now));
for(i=1; i<=4; i++)
a[i]=rand()%10;
while(a[2]==a[1])
a[2]=rand()%10;
while(a[3]==a[1] || a[3]==a[2])
a[3]=rand()%10;
while(a[4]==a[1] || a[4]==a[2] || a[4]==a[3])
a[4]=rand()%10;
printf("Guess a four-digit number that randomly given by the computer.\nYou have 8 times to guess it.\n");
for(k=1; k<=8; k++){
right=match=0;
scanf("%d", &n);
for(j=4; j>=1; j--){
b[j]=n%10;
n=(n-b[j])/10;
if(a[j]==b[j]){
right++;}
for(i=1; i<=4; i++){
if(a[i]==b[j] && i!=j){
match++;}
}
}
if(right==4){
printf("Bingo! You got it!\n"); break;}
else
printf("%dA %dB\t Try again!\n",right, match);
}
if(right!=4){
printf("\nWhat a pity. You nearly made it!\n The correct answer is %d%d%d%d\n", a[1], a[2], a[3], a[4]);}
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main()
{
int a[4], b[4]; int i, j, k, n, right, match; long now;
srand(time(&now));
for(i=1; i<=4; i++)
a[i]=rand()%10;
while(a[2]==a[1])
a[2]=rand()%10;
while(a[3]==a[1] || a[3]==a[2])
a[3]=rand()%10;
while(a[4]==a[1] || a[4]==a[2] || a[4]==a[3])
a[4]=rand()%10;
printf("Guess a four-digit number that randomly given by the computer.\nYou have 8 times to guess it.\n");
for(k=1; k<=8; k++){
right=match=0;
scanf("%d", &n);
for(j=4; j>=1; j--){
b[j]=n%10;
n=(n-b[j])/10;
if(a[j]==b[j]){
right++;}
for(i=1; i<=4; i++){
if(a[i]==b[j] && i!=j){
match++;}
}
}
if(right==4){
printf("Bingo! You got it!\n"); break;}
else
printf("%dA %dB\t Try again!\n",right, match);
}
if(right!=4){
printf("\nWhat a pity. You nearly made it!\n The correct answer is %d%d%d%d\n", a[1], a[2], a[3], a[4]);}
}
相关日志:
收藏:
QQ书签
del.icio.us
订阅:
Google
抓虾
最新评论
-
2006-05-01 13:31:48 http://spaces.msn.com/xukongwangyue/
强,这东西我还编不出!!!
-
2007-06-27 16:35:23 匿名 220.201.*.*







































