1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Atcoder解いてみた ABC 079 C - Train Ticket

Posted at

ABC 079 C - Train Ticket

問題はこちら

回答

#include <iostream>
#include <string>
#include <map>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;


int main() {
    // 0<=, <=9

    string S;

    cin >> S;

    // 答えが出たらbreak
    for (int d1 = 0; d1 < 2; d1++) {
        for (int d2 = 0; d2 < 2; d2++) {
            for (int d3 = 0; d3 < 2; d3++) {
                int tmp1;
                int tmp2;
                int tmp3;

                if (d1 == 0) tmp1 = (S[0] - '0') + (S[1] - '0');
                else if (d1 == 1) tmp1 = (S[0] - '0') - (S[1] - '0');

                if (d2 == 0) tmp2 = tmp1 + (S[2] - '0');
                else if (d2 == 1) tmp2 = tmp1 - (S[2] - '0');

                if (d3 == 0) tmp3 = tmp2 + (S[3] - '0');
                else if (d3 == 1) tmp3 = tmp2 - (S[3] - '0');

                if (tmp3 == 7) {
                    std::cout << S[0];

                    if (d1 == 0) std::cout << '+';
                    else if (d1 == 1) std::cout << '-';

                    std::cout << S[1];

                    if (d2 == 0) std::cout << '+';
                    else if (d2 == 1) std::cout << '-';

                    std::cout << S[2];

                    if (d3 == 0) std::cout << '+';
                    else if (d3 == 1) std::cout << '-';

                    std::cout << S[3];

                    cout << "=7" << endl;
                    return 0;
                }
            } //d3
        } //d2
    } //d1
    return 0;
}

注意

式を全ての文字列で出すとWAになってしまった、、、
=7は文字列で良かったが、それ以外の記号と数字は全て文字でださないとダメだった、、、

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?