ABC079 C - Train Ticket

Calendar Clock iconCalendar Clock icon

atcoder

Table of contents

# Problem

https://atcoder.jp/contests/abc079/tasks/abc079_c

Given 4 integers 0A,B,C,D90 \geq A, B, C, D \geq 9, put 3 add or sub operators.
Report a combination of them whose result is 7.

# Time complexity

O(1)O(1)

# Solution

// C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int int
#define loop(x, start, end) for(Int x = start; x < end; x++)
#define loopdown(x, start, end) for(int x = start; x > end; x--)
#define rep(n) for(int x = 0; x < n; x++)
#define span(a,x,y) a.begin()+x,a.begin()+y
#define span_all(a) a.begin(),a.end()
#define len(x) (x.size())
#define last(x) (*(x.end()-1))

using namespace std;
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;
}

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!

Offer jobs or contact me!

Comments