-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake10.cpp
More file actions
160 lines (143 loc) · 5.44 KB
/
make10.cpp
File metadata and controls
160 lines (143 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <math.h>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
// 2つの数の演算を指定された演算子によって行う
double cal(double first, double second, int op) {
switch (op) {
case 0:
return first + second;
break;
case 1:
return first - second;
break;
case 2:
return first * second;
break;
case 3:
return first / second;
break;
default:
break;
}
return 0;
}
// 数字で管理している演算子を文字に直す関数
string change(int op) {
switch (op) {
case 0:
return "+";
break;
case 1:
return "-";
break;
case 2:
return "×";
break;
case 3:
return "÷";
break;
default:
break;
}
return 0;
}
int main() {
// 割り算を扱うため doubleで宣言
vector<double> input(4);
// 入力を受け取る
cout << "4つの数(0〜9)をスペースありで入力してください.(例:1 2 3 4)"
<< endl;
cin >> input[0] >> input[1] >> input[2] >> input[3];
cout << endl;
// 配列をsortする
sort(input.begin(), input.end());
// 正解があるかをチェック
bool check = false;
// next_permutationを用いて順列の全探索
cout << "<解答一覧>" << endl;
while (1) {
// 4つの数の間に入る演算子を全探索
for (int op1 = 0; op1 < 4; op1++) {
for (int op2 = 0; op2 < 4; op2++) {
for (int op3 = 0; op3 < 4; op3++) {
double ans = 0;
// pattern1[((n1 op1 n2) op2 n3) op3 n4]
ans = cal(input[0], input[1], op1);
ans = cal(ans, input[2], op2);
ans = cal(ans, input[3], op3);
// 正解があれば出力
if (ans == 10) {
cout << "((" << input[0] << change(op1) << input[1]
<< ")";
cout << change(op2) << input[2] << ")";
cout << change(op3) << input[3] << endl;
check = true;
}
// pattern2[(n1 op1 n2) op2 (n3 op3 n4)]
double temp1 = 0, temp2 = 0;
temp1 = cal(input[0], input[1], op1);
temp2 = cal(input[2], input[3], op3);
ans = cal(temp1, temp2, op2);
// 正解があれば出力
if (ans == 10) {
cout << "(" << input[0] << change(op1) << input[1] << ")";
cout << change(op2);
cout << "(" << input[2] << change(op3) << input[3] << ")" << endl;
;
check = true;
}
// pattern3[割り算の例外処理]
//double temp1 = 0, temp2 = 0;
if (op1 == 3 || op2 == 3 || op3 == 3) {
// n4 op3 (n3 op2 (n1 op1 n2))
ans = cal(input[0], input[1], op1);
ans = cal(input[2], ans, op2);
ans = cal(input[3], ans, op3);
// 正解があれば出力
if (ans == 10) {
cout << input[3] << change(op3);
cout << "(" << input[2] << change(op2);
cout << "(" << input[0] << change(op1) << input[1] << "))" << endl;
check = true;
}
} else if (op1 == 3 || op2 == 3) {
// (n3 op2 (n1 op1 n2)) op3 n4
ans = cal(input[0], input[1], op1);
ans = cal(input[2], ans, op2);
ans = cal(ans, input[3], op3);
// 正解があれば出力
if (ans == 10) {
cout << "(" << input[2] << change(op2);
cout << "(" << input[0] << change(op1) << input[1]
<< "))";
cout << change(op3) << input[3] << endl;
check = true;
}
} else if (op1 == 3 || op3 == 3) {
// n4 op3 ((n1 op1 n2) op2 n3)
ans = cal(input[0], input[1], op1);
ans = cal(ans, input[2], op2);
ans = cal(input[3], ans, op3);
// 正解があれば出力
if (ans == 10) {
cout << input[3] << change(op3) << endl;
cout << "((" << input[0] << change(op1) << input[1]
<< ")";
cout << change(op2) << input[2] << ")" << endl;
check = true;
}
} else {
continue;
}
}
}
}
// falseを返した時に探索を終了
if (!next_permutation(input.begin(), input.end())) break;
}
// 該当なし
if (!check) cout << "Not Found" << endl;
return 0;
}