本文共 1083 字,大约阅读时间需要 3 分钟。
急用“人机猜数游戏”的总流程图,if和for循环的部分流程图
*程序与程序注释
#include
#include
#include
void main()
{
int s
急用“人机猜数游戏”的总流程图,if和for循环的部分流程图
*程序与程序注释
#include
#include
#include
void main()
{
int stime,a,z,t,i,c,m,g,s,j,k,l[4]; /*j:数字正确的位数 k:位置正确的位数*/
long ltime;
ltime=time(NULL); /*l:数字相同时,人所猜中数字的正确位置*/
stime=(unsigned int)ltime/2;
srand(stime);
z=random(9999); /*计算机想一个随机数*/
printf("I have a number with 4 digits in mind,please guess.\n");
for(c=1;;c++) /*c: 猜数次数计数器*/
{
printf("Enter a number with 4 digits:");
scanf("%d",&g); /*请人猜*/
a=z;j=0;k=0;l[0]=l[1]=l[2]=l[3]=0;
for(i=1;i<5;i++) /*i:原数中的第i位数。个位为第一位,千位为第4位*/
{
s=g;m=1;
for(t=1;t<5;t++) /*人所猜想的数*/
{
if(a%10==s%10) /*若第i位与人猜的第t位相同*/
{
if(m&&t!=l[0]&&t!=l[1]&&t!=l[2]&&t!=l[3])
{
j++;m=0;l[j-1]=t; /*若该位置上的数字尚未与其它数字“相同”*/
} /*记录相同数字时,该数字在所猜数字中的位置*/
if(i==t) k++; /*若位置也相同,则计数器k加1*/
}
s/=10;
}
a/=10;
}
printf("You hane correctly guessed %d digits,\n",j);
printf("and correctly guessed %d digits in exact position.\n",k);
if(k==4) break; /*若位置全部正确,则人猜对了,退出*/
}
printf("Now you have correctly guessed the whole number after %d times.\n",c);
}
展开
全部
转载地址:https://blog.csdn.net/weixin_34518190/article/details/117157216 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!