結果

問題 No.297 カードの数式
ユーザー h_nosonh_noson
提出日時 2016-02-17 13:40:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,473 bytes
コンパイル時間 1,691 ms
コンパイル使用メモリ 70,268 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 22:53:01
合計ジャッジ時間 2,006 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#define RREP(i,s,e) for (int i = e-1; i >= s; i--)
#define rrep(i,n) RREP(i,0,n)
#define REP(i,s,e) for (int i = s; i < e; i++)
#define rep(i,n) REP(i,0,n)

long long calc(string s) {
    if (s.empty())
        return 0;
    size_t len;
    long long x = stol(s,&len);
    return x + calc(s.substr(len));
}

int main() {
    int n;
    cin >> n;
    string op;
    string num;
    rep(i,n) {
        char c;
        cin >> c;
        if (isdigit(c))
            num.push_back(c);
        else
            op.push_back(c);
    }
    sort(op.begin(),op.end());
    sort(num.begin(),num.end(),greater<char>{});
    int pos = num.size() - op.size();
    string ans = num.substr(0,pos);
    for (auto p = op.begin(), q = num.begin() + pos; p != op.end(); p++, q++) {
        ans.push_back(*p);
        ans.push_back(*q);
    }
    cout << calc(ans) << " ";
    ans.clear();
    if (op.back() == '-') {
        auto p = op.begin();
        auto q = num.rbegin();
        for (; p != op.end(); p++, q++) {
            ans.push_back(*q);
            ans.push_back(*p);
        }
        ans.append(num.substr(0,pos));
    }
    else {
        rep (i,op.size()+1) {
            for (int j = num.size()-i-1; j >= 0; j-=op.size()+1)
                ans.push_back(num[j]);
            if (i < op.size())
                ans.push_back('+');
        }
    }
    cout << calc(ans) << endl;
    return 0;
}

0