結果

問題 No.297 カードの数式
ユーザー face4face4
提出日時 2018-09-20 17:21:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,305 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 76,476 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-25 10:34:24
合計ジャッジ時間 1,723 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

typedef long long ll;

int main(){
    int n;
    cin >> n;

    char c;
    int plus = 0, minus = 0;
    vector<int> v;

    for(int i = 0; i < n; i++){
        cin >> c;
        if(c == '+')    plus++;
        else if(c == '-')   minus++;
        else            v.push_back(c-'0');
    }

    sort(v.begin(), v.end());

    ll mi = 0, ma = 0;
    
    // max
    int pos = 0;
    for(int i = 0; i < minus; i++)  ma -= v[pos], pos++;
    for(int i = 0; i < plus; i++)   ma += v[pos], pos++;
    ll largest = 0;
    for(int i = v.size()-1; i >= pos; i--) largest = 10*largest + v[i];
    ma += largest;

    // min
    if(minus == 0){
        reverse(v.begin(), v.end());
        int numofnum = plus + 1;
        for(int i = 0; i < v.size(); i++){
            ll times = 1;
            for(int j = 0; j < i/numofnum; j++) times *= 10;
            mi += times * v[i];
        }
    }else{
        mi = v[0];
        pos = 1;
        for(int i = 0; i < plus; i++)   mi += v[pos], pos++;
        for(int i = 1; i < minus; i++)  mi -= v[pos], pos++;
        largest = 0;
        for(int i = v.size()-1; i >= pos; i--) largest = 10*largest + v[i];
        mi -= largest;
    }

    cout << ma << " " << mi << endl;

    return 0;
}
0