結果

問題 No.81 すべて足すだけの簡単なお仕事です。
ユーザー mamekinmamekin
提出日時 2015-01-17 16:55:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,931 bytes
コンパイル時間 1,460 ms
コンパイル使用メモリ 91,872 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-29 22:27:39
合計ジャッジ時間 2,047 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

pair<long long, long long> add(long long a1, long long b1, long long a2, long long b2)
{
    a1 += a2;
    b1 += b2;
    if(b1 >= 10000000000LL){
        ++ a1;
        b1 -= 10000000000LL;
    }
    return make_pair(a1, b1);
}

pair<long long, long long> sub(long long a1, long long b1, long long a2, long long b2)
{
    a1 -= a2;
    b1 -= b2;
    if(b1 < 0){
        -- a1;
        b1 += 10000000000LL;
    }
    return make_pair(a1, b1);
}

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

    bool sign = true;
    long long a = 0;
    long long b = 0;
    for(int i=0; i<n; ++i){
        string s;
        cin >> s;
        int k = s.find('.');

        bool sign2 = s[0] != '-';
        long long a2, b2;
        if(k == string::npos){
            if(sign2)
                a2 = stoll(s);
            else
                a2 = stoll(s.substr(1));
            b2 = 0;
        }
        else{
            string x = s.substr(0, k);
            string y = s.substr(k+1);
            y.resize(10, '0');
            if(sign2)
                a2 = stoll(x);
            else
                a2 = stoll(x.substr(1));
            b2 = stoll(y);
        }

        if(sign == sign2){
            tie(a, b) = add(a, b, a2, b2);
        }
        else if(make_pair(a, b) < make_pair(a2, b2)){
            sign = sign2;
            tie(a, b) = sub(a2, b2, a, b);
        }
        else{
            tie(a, b) = sub(a, b, a2, b2);
        }
    }

    if(!sign)
        cout << '-';
    cout << a << '.' << setfill('0') << setw(10) << b << endl;

    return 0;
}
0