- 猜数字-
小明和电脑比赛,电脑随机出一个数字(1-100),小明猜对的次数在3次以内(包含3次),小明赢,否则电脑赢。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num = rand() % 100 + 1;
int n;
for (int i = 0; i < 3; i++)
{
scanf("%d",&n);
if (n==num)
{
printf("小明赢");
break;
}
}
if (n != num)
{
printf("电脑赢");
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int y = 0, n = 0, i = 0;
while(1)
{
int xm;
int num = rand() % 100 + 1;
cout << "请小明输入:";
cin >> xm;
if(xm == num)
{
y++;
cout << "小明猜对了。" << endl;
}
else
{
n++;
cout << "小明猜错了。" << endl;
}
if(y >= 3)
{
cout << "小明获胜。" << endl;
break;
}
if(n >= 3)
{
cout << "电脑获胜。" << endl;
break;
}
}
return 0;
}
