您的位置:首页 > 其它

小游戏——猜数字

2014-06-28 04:03 162 查看
简单的随机数应用

有兴趣的可以玩玩

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

#define N 4/*随机抽N个数*/
#define MAX 50//最大尝试数目
int main()
{
int i;
int count;//次数
int s_passw[N+1];
void detect(int[],int);
srand(time(NULL));
for(i=0;i<N;i++)
{
s_passw[i]=rand()%10;//得到随机数
}
printf("****\n");
printf("自信的你想要猜几次呢: ");
scanf("%d",&count);//输入次数
while(1)
{
if(count>MAX)
{
printf("太多了,少一点: ");
scanf("%d",&count);//输入次数
}
else
break;
}
detect(s_passw,count);
return 0;
}
void get_number(char &a,int &b)//类型转换
{
b=a-'0';
}
void detect(int right[],int count)
{
int i,j,num;
int times=0,guess_i[N+1];
char guess_c[N+1];
for(num=0;num<count;num++)//机会
{
times=0;
printf("第%d次机会: ",num+1);
scanf("%s",guess_c);//得到值
for(i=0;i<N;i++)
{
get_number(guess_c[i],guess_i[i]);
}
for(j=0;j<N;j++)//检查跟正确值的差异
{
if(guess_i[j]==right[j])
{
printf("1");
times++;
}
else
printf("0");
}
printf("\n");
if(times==N)//4个正确
{
printf("Congratulations!\n");
break;
}
}
if(times!=N)//一直不正确
{
printf("Sorry,you haven't guess the right number!\nThe correct answer is:");
for(j=0;j<N;j++)
{
printf("%d",right[j]);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: