C++猜数游戏

2024-11-26 17:00:18
推荐回答(4个)
回答1:

#include
using namespace std;
#include
#include

int main()
{
int x;
cout<<"输入任意数字开始。"< while(cin>>x)
{
srand((int)time(0));
int m = rand()%100+1;//生成随机数m范围1到100
int i =0,n;
for (;i<10;i++)//判断是否才对,10次为限,i用来计算才对次数
{
cout<<"input the number you guess:"< cin>>n;
if( n > m )
cout<<"wrong,too high!"< if( n < m )
cout<<"wrong,too low!"< if( n == m)
{cout<<"right,and the times you have guessed is"< }
cout<<"输入任意数字继续。"< }
system("pause");
return 0;
}

回答2:

#include
#include
#define MAX 10
void guess(int n)
{
int i;
int g;

for (i = 0; i < MAX; i++)
{
printf("Input the number(1~100):");
scanf("%d", &g);
if (g < 1 || g > 100)
{
printf("Please input the number between 1 ~ 100:");
continue;
}
else if (g > n)
{
printf("Wrong!Too high.\n");
}
else if (g < n)
{
printf("Wrong!Too low.\n");
}
else
{
printf("Right!\n");
return;
}
}

printf("You lose!\n");
}

void main()
{
int n;
char c = ' ';

srand(time(NULL));

do
{
n = rand() % 100 + 1;
guess(n);

printf("Continue? (y or n):");
c = getchar();
while (c == ' ' || c == '\n') c = getchar();
} while (c == 'y' || c == 'Y');
}

回答3:

dio.h"
#include"stdlib.h"
#include"time.h"
int A=0,B=0;
int pd(int a[])/*判断是否有相同位相等与否*/
{int i,j,leap=1;
for(i=0;i<3;i++)
for(j=i+1;j<4;j++)
if(a[i]==a[j])
leap=0;
return leap;
}
int sl(int a)/*判断输入的数字是否合法*/
{int b=0,leap=0,dl[4];
if(a>=1000&&a<=9999)
{dl[0]=a/1000;
dl[1]=a%1000/100;
dl[2]=a%100/10;
dl[3]=a%10;
b=pd(dl);/*判断四位数的合法性*/
}
if(b==1)
leap=1;
return leap;
}

void chuli(int a[],int b[])/*判断猜测情况*/
{int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i]==b[j])
{
printf("%d 对,",b[j]);
if(i==j)
printf("位置也对!\n");
else printf("但位置不对!\n");
}
}

void main()
{int a[4],b[4],k,m,z,i=0,j,c;
printf(" 这是一个猜数字的游戏,根据系统提示猜一个四位数。\n (注意:正确的数字是每一位都不重复的四位数)祝你好运~~\n");
while(1)
{srand((unsigned)time(NULL));
z=(rand()%100)*(rand()%100);
if(sl(z))
{k=z;
break;
}
}
for(j=3;j>=0;j--)

while(1)
{ printf("输入你猜的数字:\n");
scanf("%d",&m);
if(m==z)
{ printf("恭喜你答对了O(∩_∩)O\n");
break;
}
i++;
for(j=3;j>=0;j--)

chuli(a,b);
if(i==8)
{ printf("你都错了8次了,还要猜吗?(1:yes,2:no)\n");
scanf("%d",&c);
if(c==2)
break;
}
else if(i==15)
{
printf("别猜了,正确答案:%d\n",k);
break;
}
else
printf("猜的不正确,还差点,加油啊!\n");

}
}
另外,虚机团上产品团购,超级便宜

回答4:

67