- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- string poker[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
- char C[12] = { '+', '+', '+', '-', '-', '-', '*', '*', '*', '/', '/', '/' };
- int Calculate(int a, int b, char c)
- {
- switch (c){
- case '+': return a + b;
- case '-': return a - b;
- case '*': return a * b;
- case '/': return a / b;
- default: return -1; //不会有这个选项
- }
- }
- int Caculate24(int a, int b, int c, int d, char C1, char C2, char C3)
- {
- return Calculate(Calculate(Calculate(a, b, C1), c, C2), d, C3);
- }
- bool Count24(int a, int b, int c, int d)
- {
- sort(C, C + 12);
- do {
- if (Caculate24(a, b, c, d, C[0], C[1], C[2]) == 24){
- //do right;
- return true;
- }
- } while (next_permutation(C, C + 12));
- return false;
- }
- int main()
- {
- string str[4];
- cin >> str[0] >> str[1] >> str[2] >> str[3];
- int i, j;
- bool legal;
- for (i = 0; i < 4; i++){
- legal = false;
- for (j = 0; j < 13; j++){
- if (str[i].compare(poker[j].c_str()) == 0)
- legal = true;
- }
- if (legal == false){
- cout << "ERROR" << endl;
- return 0;
- }
- }
- int a, b, c, d;
- string *p;
- p = find(poker, poker + 13, str[0]);
- a = p - poker + 1;
- p = find(poker, poker + 13, str[1]);
- b = p - poker + 1;
- p = find(poker, poker + 13, str[2]);
- c = p - poker + 1;
- p = find(poker, poker + 13, str[3]);
- d = p - poker + 1;
- //现在a, b, c, d分别代表每一张牌的数值
- int num[4];
- num[0] = a, num[1] = b, num[2] = c, num[3] = d;
- do {
- if (Count24(num[0], num[1], num[2], num[3])){
- cout << poker[num[0] - 1] << C[0] << poker[num[1] - 1] << C[1] << poker[num[2] - 1] << C[2] << poker[num[3] - 1] << endl;
- return 0;
- }
- } while (next_permutation(num, num+4));
- cout << "NONE" << endl;
- return 0;
- }
24点运算
最新推荐文章于 2020-12-05 12:03:30 发布