24 Game
2017-09-24 19:16
288 查看
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through
get the value of 24.
Example 1:
Example 2:
Note:
The division operator
example, 4 / (1 - 2/3) = 12.
Every operation done is between two numbers. In particular, we cannot use
a unary operator. For example, with
You cannot concatenate numbers together. For example, if the input is
列出四个数的全排列,对每一个排列进行所有可能的运算(三个位置的运算符加减乘除有4*4*4=64种可能,插入括号有6种方式),检查运算结果是否为24。
#include<iostream>
#include<vector>
#include <cmath>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<double> operands;
bool judgePoint24(vector<int>& nums) {
for (int i = 0 ;i < 4; i++)
operands.push_back(nums[i]);
for (int i = 0; i < 24; i++) {
for (int x = 1; x <= 4; x++)
for (int y = 1; y <= 4; y++)
for (int z = 1; z <= 4; z++) {
double left,middle,right,result;
left=compute(x, operands[0], operands[1]);
middle=comp
4000
ute(y, operands[1],operands[2]);
right=compute(z,operands[2], operands[3]);
result=compute(y,left,operands[2]);
result=compute(z,result,operands[3]);
if (fabs(result-24)<0.001) return true;//a?b?c?d
result=compute(y, left,right);
if (fabs(result-24)<0.001) return true;//(a?b)?(c?d)
result=compute(x, operands[0], middle);
result=compute(z, result, operands[3]);//(a?(b?c))?d
if (fabs(result-24)<0.001) return true;
result=compute(z, middle,operands[3]);
result=compute(x, operands[0],result);//a?((b?c)?d)
if (fabs(result-24)<0.001) return true;
result= compute(y,left,right);
if (fabs(result-24)<0.001) return true;//a?b?(c?d)
result=compute(y,operands[1],right);
result=compute(x,operands[0],result);
if (fabs(result-24)<0.001) return true;//a?(b?(c?d))
}
next_permutation(operands.begin(),operands.end());
}
return false;
}
double compute(int oper, double & a, double& b) {
if (oper==1) return a+b;
if (oper==2) return a-b;
if (oper==3) return a*b;
if (oper==4) {
if (b==0) return 0;
return a/b;
}
}
};
*,
/,
+,
-,
(,
)to
get the value of 24.
Example 1:
Input: [4, 1, 8, 7] Output: True Explanation: (8-4) * (7-1) = 24
Example 2:
Input: [1, 2, 1, 2] Output: False
Note:
The division operator
/represents real division, not integer division. For
example, 4 / (1 - 2/3) = 12.
Every operation done is between two numbers. In particular, we cannot use
-as
a unary operator. For example, with
[1, 1, 1, 1]as input, the expression
-1 - 1 - 1 - 1is not allowed.
You cannot concatenate numbers together. For example, if the input is
[1, 2, 1, 2], we cannot write this as 12 + 12.
列出四个数的全排列,对每一个排列进行所有可能的运算(三个位置的运算符加减乘除有4*4*4=64种可能,插入括号有6种方式),检查运算结果是否为24。
#include<iostream>
#include<vector>
#include <cmath>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<double> operands;
bool judgePoint24(vector<int>& nums) {
for (int i = 0 ;i < 4; i++)
operands.push_back(nums[i]);
for (int i = 0; i < 24; i++) {
for (int x = 1; x <= 4; x++)
for (int y = 1; y <= 4; y++)
for (int z = 1; z <= 4; z++) {
double left,middle,right,result;
left=compute(x, operands[0], operands[1]);
middle=comp
4000
ute(y, operands[1],operands[2]);
right=compute(z,operands[2], operands[3]);
result=compute(y,left,operands[2]);
result=compute(z,result,operands[3]);
if (fabs(result-24)<0.001) return true;//a?b?c?d
result=compute(y, left,right);
if (fabs(result-24)<0.001) return true;//(a?b)?(c?d)
result=compute(x, operands[0], middle);
result=compute(z, result, operands[3]);//(a?(b?c))?d
if (fabs(result-24)<0.001) return true;
result=compute(z, middle,operands[3]);
result=compute(x, operands[0],result);//a?((b?c)?d)
if (fabs(result-24)<0.001) return true;
result= compute(y,left,right);
if (fabs(result-24)<0.001) return true;//a?b?(c?d)
result=compute(y,operands[1],right);
result=compute(x,operands[0],result);
if (fabs(result-24)<0.001) return true;//a?(b?(c?d))
}
next_permutation(operands.begin(),operands.end());
}
return false;
}
double compute(int oper, double & a, double& b) {
if (oper==1) return a+b;
if (oper==2) return a-b;
if (oper==3) return a*b;
if (oper==4) {
if (b==0) return 0;
return a/b;
}
}
};
相关文章推荐
- LINTCODE——Compute 24 Game
- Codeforces Round#268(Div 2)C 24 Game
- Codeforces Round 469 C 24 Game
- Making a mobile game in 24 hours
- cf468A 24 Game
- C. 24 Game
- codeforces 468A. 24 Game 解题报告
- LeetCode week 3 : 24 Game
- Sams teach yourself game programming in 24 hours
- cf468A 24 Game
- leetcode 24 Game
- Educational Codeforces Round 24 E. Card Game Again(双指针)
- CF 268C 24 Game
- Codeforces Round #268 (Div. 2) C 24 Game [构造]
- 24 Game CodeForces - 468A
- CodeForces-468A-24 Game
- LeetCode 679: 24 Game 解题与思考
- cf 24 Game (观察+.. 想一想)
- CodeForces 469C - 24 Game
- Codeforces 469C 24 Game【思维+模拟】