# 0ms c++ solution

• It's not a general solution, it generates all the possible combination by hard coding

``````class Solution {
public:
//char ops[6] = {'+','*','/','\\','-','_'};
double calc(double a, double b, int op) {
if (op == 0) return a+b;
else if (op == 1) return a*b;
else if (op == 2 && b) return a/b;
else if (op == 3 && a) return b/a;
else if (op == 4) return a-b;
else if (op == 5) return b-a;
return 3.141592653589793;
}

bool solve2(double a, double b) {
for (int op = 0; op < 6; op++) {
if (abs(calc(a,b,op)-24) < 1e-9) {
//printf("(%f, %f, %c) = %f \n", a,b, ops[op],calc(a,b,op));
return 1;
}
}
return 0;
}

bool solve3(double a, double b, double c) {
for (int op = 0; op < 6; op++) {
if (solve2(calc(a,b,op), c)) {
//printf("((%f, %f, %c), %f) \n", a,b, ops[op],c);
return 1;
}
}
for (int op = 0; op < 6; op++) {
if (solve2(calc(a,c,op), b)) {

//printf("((%f, %f, %c), %f)\n", a,c, ops[op],b);
return 1;
}
}
for (int op = 0; op < 6; op++) {
if (solve2(calc(b,c,op), a)) {
//printf("((%f, %f, %c), %f)\n", b, c, ops[op],a);
return 1;
}
}
return 0;
}

bool judgePoint24(vector<int>& ns) {
vector<vector<int>> a =  {{0,1},{0,2},{0,3},{1,2},{1,3},{2,3}};
for (int i = 0; i < 6; i++) {
for (int op = 0; op < 6; op++) {
double x = calc(ns[a[i][0]], ns[a[i][1]], op);
if (solve3(x, ns[a[5-i][0]], ns[a[5-i][1]])) return 1;
}
}
return 0;
}
};
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.