猜数字
要求:
1.随机四个数(0-9无重复的有序的)
2.从键盘中输入四个数
3.输入的数和随机数进行比较,按照比较的结果给出提示信息
提示信息: 随机数 5 0 7 2
输入数 5 3 2 7
1A2B:A 完全正确 B数值正确,位置不正确。
4.如果为4A,结束游戏,否则重复2-3步骤5次。
#include<time.h>
using namespace std;
int main()
{
int i,j,m=0,arr[4],get[4];
srand((unsigned)time(NULL));
for (i = 0; i < 4; i++)
{
arr[i] = rand() % 10;//随机生成4个0——9之间的数;
for (int k = 0; k < i; k++)
if (arr[k] == arr[i])
i--;
}
for (int i = 0; i < 4; i++)
{
cout << arr[i] ;
}
cout << endl;
//cin >> arr[0] >> arr[1] >> arr[2] >> arr[3];
int a, b, c, d;
int A, B,C;
cout << "游戏:猜数字" << endl;
cout << " ";
cout << "请猜四个不相同的数字" << endl;
cout << " ";
cout << "您共有6次机会" << endl;
while (m<6)
{
m++;
cout << "第" << m << "次输入!" << endl;
cin >> a >> b >> c >> d;
if (a == b || a == c || a == d || b == c || b == d || c == d)
{
cout << "请不要输入相同的数" << endl;
cout << "请重新输入。。。" << endl;
continue;
}
A = 0;
B = 0;
C = 0;
if (a == arr[0])
{
A++;
}
else
{
C++;
if (a == arr[1] || a == arr[2] || a == arr[3])
B++;
}
if (b == arr[1])
{
A++;
}
else
{
C++;
if (b == arr[0] || b == arr[2] || b == arr[3])
B++;
}
if (c == arr[2])
{
A++;
}
else
{
C++;
if (c == arr[1] || c == arr[0] || c == arr[3])
B++;
}
if (d == arr[3])
{
A++;
}
else
{
C++;
if (d == arr[1] || d == arr[2] || d == arr[0])
B++;
}
/*if (a != arr[0] &&( a == arr[1] || a == arr[2] || a == arr[3]))
B++;
if (b!= arr[1] && (b == arr[0] || b == arr[2] || b == arr[3]))
B++;
if (c != arr[2] &&( c == arr[1] || c == arr[0] || c == arr[3]))
B++;
if (d != arr[3] &&( d == arr[1] || d == arr[2] || d == arr[0]))
B++;
*/
cout << A << "A" << C << "C" <<B << "B" << endl;
if (4 == A)
{
cout << "恭喜你猜对了!" << endl;
break;
}
else
{
if (0 == (6 - m))
{
cout << "您答对了" << A << "个数字;" << "答错了" << C << "个。其中" << B << "个位置错了;" << endl;
cout << "而且您的次数已经用完,请重启!" << endl;
break;
}
else
{
cout << "您答对了" << A << "个数字;" << "答错了" << C << "个。其中" << B << "个位置错了;" << endl;
cout << "您还有" << 6 - m << "次机会,请再接再厉!" << endl;
cout << "-------------------------------------------------------------" << endl;
continue;
}
}
}
system("pause");
return 0;
}