ABC079 C - Train Ticket
Table of contents
# Problem
https://atcoder.jp/contests/abc079/tasks/abc079_c
Given 4 integers , put 3 add or sub operators.
Report a combination of them whose result is 7.
# Time complexity
# Solution
string S;
char ops[] = { '-', '+' };
void input() {
cin >> S;
}
#define ctoi(x) (x - '0')
void solve() {
Int x = 0;
loop(first, 0, 2) {
loop(second, 0, 2) {
loop(third, 0, 2) {
x = ctoi(S[0]);
if (first) x += ctoi(S[1]);
else x -= ctoi(S[1]);
if (second) x += ctoi(S[2]);
else x -= ctoi(S[2]);
if (third) x += ctoi(S[3]);
else x -= ctoi(S[3]);
if (x == 7) {
cout << S[0] << ops[first] << S[1] << ops[second] << S[2] << ops[third] << S[3] << "=7" << endl;
return;
}
}
}
}
cout << -1 << endl;
}
int main(void) {
input();
solve();
return 0;
}
Shun
Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!