题目:
Description
Input
Output
Sample Input
Sample Output
在 24点游戏综述中有提到,24点分2大类,一种是除法可以出现分数的,一种是不可以的,
本题是不可以的,在CSU 1600: Twenty-four point (24点)这个题目中是可以的
主要的思路就是函数的嵌套。
这个题目我是只用一个字符ch记录所有输入的,空格会自动忽略,0我就人为忽略,遇到0就读取下一个字符,这样,A就是表示1,1就是表示10
代码:
#include
using namespace std;
int number(char c)
{
if (c == 'A')return 1;
if (c == 'J')return 11;
if (c == 'Q')return 12;
if (c == 'K')return 13;
if (c == '1')return 10;
return c - '0';
}
bool ok(int a, int b)
{
if (a + b == 24)return true;
if (a - b == 24)return true;
if (a*b == 24)return true;
if (a != 0 && a == 24 * b)return true;
return false;
}
bool ok(int a, int b, int c)
{
if (b == 0)
{
if (ok(a, c))return true;
return false;
}
if (c == 0)
{
if (ok(a, b))return true;
return false;
}
if (ok((a + b), c) || ok((a - b), c) || ok((a * b), c))return true;
if (a%b == 0 && ok(a / b, c))return true;
if (ok(a, b + c) || ok(a, b - c) || ok(a, b * c))return true;
if (b%c == 0 && ok(a, b / c))return true;
return false;
}
bool ok(int a, int b, int c, int d)
{
if (ok(a + b, c, d) || ok(a - b, c, d) || ok(a * b, c, d))return true;
if (a%b == 0 && ok(a / b, c, d))return true;
if (ok(a, b + c, d) || ok(a, b - c, d) || ok(a, b * c, d))return true;
if (b%c == 0 && ok(a, b / c, d))return true;
if (ok(a, b, c + d) || ok(a, b, c - d) || ok(a, b, c * d))return true;
if (c%d == 0 && ok(a, b, c / d))return true;
return false;
}
bool ok1(int a, int b, int c, int d)
{
if (ok(a, b, c, d) || ok(a, c, b, d) || ok(b, a, c, d) || ok(b, c, a, d) || ok(c, a, b, d) || ok(c, b, a, d))return true;
return false;
}
int main()
{
char ch;
int a, b, c, d;
while (cin >> ch)
{
if (ch == '0')cin >> ch;
a = number(ch);
cin >> ch;
if (ch == '0')cin >> ch;
b = number(ch);
cin >> ch;
if (ch == '0')cin >> ch;
c = number(ch);
cin >> ch;
if (ch == '0')cin >> ch;
d = number(ch);
if (ok1(a, b, c, d)||ok1(b,c,d,a)||ok1(c,d,a,b)||ok1(d,a,b,c))cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}