規則:
1.隨機產生一個四位數,數字間不得重複。
2.讓使用者只能輸入四位數,且數字間不得重複。
3.若使用者猜對一數字和數字之位置,得1A。
4.若使用者猜對一數字,卻沒猜中數字之位置,得1B。
5.不得使用陣列。
============================================================================
以下:
#include<iostream>
using namespace std;
#include<cstdlib>
#include<ctime>
int main()
{
int a, b, c, d;
int e, f, g, h;
int number;
int guess;
do
{
srand(time(0));
a = rand() % (9) + 1;
b = rand() % (10) + 0;
c = rand() % (10) + 0;
d = rand() % (10) + 0;
} while ((a == b) || (a == c) || (a == d) || (b == c) || (b == d) || (c == d));
number = (a * 1000) + (b * 100) + (c * 10) + d;
cout << "Enter your estimate:" << endl;
cin >> guess;
while (guess != number)
{
int aa = 0, bb = 0;
e = guess / 1000;
f = (guess - e * 1000) / 100;
g = (guess - e * 1000 - f * 100) / 10;
h = guess - e * 1000 - f * 100 - g * 10;
if (guess >= 1000 && guess < 10000 && (e == f || e == g || e == h || f == g || f == h || g == h))
{
cout << "Error. 4 digits cannot be the same." << endl;
cout << "Enter a number agiin." << endl;
cin >> guess;
}
else if (guess < 1000 || guess >= 10000)
{
cout << "Error." << endl;
cout << "Enter a 4-digit number." << endl;
cin >> guess;
}
else
{
if (a == e)
{
aa = ++aa;
}
if (b == f)
{
aa = ++aa;
}
if (c == g)
{
aa = ++aa;
}
if (d == h)
{
aa = ++aa;
}
if (e == b || e == c || e == d)
{
bb = ++bb;
}
if (f == a || f == c || f == d)
{
bb = ++bb;
}
if (g == a || g == b || g == d)
{
bb = ++bb;
}
if (h == a || h == b || h == c)
{
bb = ++bb;
}
cout << "Incorrect, Please enter a number again. " << aa << "A" << bb << "B" << endl;
cin >> guess;
}
}
cout << "4A0B." << endl;
cout << "Bingo!" << endl;
system("pause");
return 0;
}