-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem B Incomplete solution.cpp
More file actions
80 lines (70 loc) · 1.31 KB
/
Problem B Incomplete solution.cpp
File metadata and controls
80 lines (70 loc) · 1.31 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
#include<vector>
#include<stdio.h>
#define middle 2001
using namespace std;
vector<int> number;
int result, n;
int sol[1000];
bool partialsol[middle * 2][1000];
int comp(int a, int b, int code){
switch (code){
case 0:
return a + b;
case 1:
return a * b;
case 2:
return a - b;
case 3:
return b == 0? 1001: a / b;
}
}
bool Find(int index, int sum){
if(index == n - 1){
if(sum == result)
return true;
}
else
for(int i = 0; i < 4; i++){
sol[index] = i;
int s = comp(sum, number[index + 1], i);
if(abs(s) > 1000)
continue;
if(!partialsol[middle + s][index])
if(Find(index + 1, s))
return true;
else
partialsol[middle + s][index] = true;
}
return false;
}
int main(){
FILE *In = fopen("input.txt", "r");
FILE *Out = fopen("output.txt", "w");
while(!feof(In)){
int x;
fscanf(In, "%d\n", &x);
number.push_back(x);
}
result = number[number.size() - 1];
number.pop_back();
n = number.size();
if(Find(0, number[0]))
for(int i = 0; i < n - 1; i++)
switch (sol[i]){
case 0:
fprintf(Out,"%s", "+");
break;
case 1:
fprintf(Out,"%s", "*");
break;
case 2:
fprintf(Out,"%s", "-");
break;
case 3:
fprintf(Out,"%s", "/");
break;
}
else
fprintf(Out, "%s", "IMPOSSIBLE");
fclose(Out);
}