結果

問題 No.81 すべて足すだけの簡単なお仕事です。
ユーザー どららどらら
提出日時 2016-05-03 23:45:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,140 bytes
コンパイル時間 1,191 ms
コンパイル使用メモリ 82,528 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-29 22:32:54
合計ジャッジ時間 2,039 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <stack>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;

struct NUM {
  ll flg;
  string n;
  
  void set(const string& str){
    int id = 0;
    if(str[id]=='-'){
      id++;
      flg = -1;
    }else{
      flg = 1;
    }

    int p = -1;
    n = "";
    REP(i,id,str.length()){
      if(str[i] == '.'){
        p = 0;
      }else{
        if(p >= 0){
          p++;
        }
        n += str[i];
      }
    }
    if(p < 0) p = 0;
    REP(i,p,10){
      n += "0";
    }
    bool leading = true;
    string tmp;
    rep(i,n.size()){
      if(leading && n[i] == '0') continue;
      leading = false;
      tmp += n[i];
    }
    n = tmp;
    if(tmp.size() == 0) n = "0";
  }
};
NUM add(NUM a, NUM b){
  if(a.flg == b.flg){
    NUM ret;
    ret.flg = a.flg;
    int sz = max(a.n.size(), b.n.size());
    sz++;
    a.n = string(sz-a.n.size(), '0') + a.n;
    b.n = string(sz-b.n.size(), '0') + b.n;
    string tmp(sz,'0');
    for(int c=0, i=sz-1; i>=0; i--){
      int ra = a.n[i]-'0';
      int rb = b.n[i]-'0';
      tmp[i] = (c=ra+rb+c/10)%10 + '0';
    }
    bool leading = true;
    rep(i,tmp.size()){
      if(leading && tmp[i] == '0') continue;
      leading = false;
      ret.n += tmp[i];
    }
    if(ret.n.size() == 0){
      ret.flg = 1;
      ret.n = "0";
    }
    return ret;
  }else{
    NUM ret;

    bool alarge = true;
    if(a.n.size() < b.n.size()) alarge = false;
    else if(a.n.size() > b.n.size()) alarge = true;
    else{
      if(a.n == b.n){
        ret.flg = 1;
        ret.n = "0";
        return ret;
      }
      rep(i,a.n.size()){
        if(a.n[i] < b.n[i]){ alarge = false; break; }
        else if(a.n[i] > b.n[i]){ alarge = true; break; }
      }
    }
    if(alarge){
      ret.flg = a.flg;
      swap(a, b);
    }else{
      ret.flg = b.flg;
    }
    //b-a
    a.n = string(b.n.size()-a.n.size(), '0') + a.n;

    string tmp(b.n.size(), '0');
    for(int c=0, i=b.n.size()-1; i>=0; i--){
      int ra = a.n[i]-'0';
      int rb = b.n[i]-'0';
      tmp[i] = ((c=rb-ra+(c-9)/10)+10)%10 + '0';
    }

    bool leading = true;
    rep(i,tmp.size()){
      if(leading && tmp[i] == '0') continue;
      leading = false;
      ret.n += tmp[i];
    }
    return ret;
  }
}


int main(){
  int N;
  cin >> N;
  NUM ret;
  ret.set("0");
  rep(i,N){
    string str;
    cin >> str;
    NUM a;
    a.set(str);
    ret = add(ret, a);
  }

  if(ret.flg<0) cout << "-";
  string tmp = string(50-ret.n.size(), '0') + ret.n;
  bool leading = true;
  rep(i,40){
    if(leading && tmp[i] == '0') continue;
    leading = false;
    cout << tmp[i];
  }
  if(leading) cout << "0";
  cout << ".";
  REP(i,40,50){
    cout << tmp[i];
  }
  cout << endl;
  return 0;
}
0