結果

問題 No.81 すべて足すだけの簡単なお仕事です。
ユーザー tkmst201tkmst201
提出日時 2017-04-10 11:58:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,110 bytes
コンパイル時間 1,889 ms
コンパイル使用メモリ 152,964 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 22:40:14
合計ジャッジ時間 3,196 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> P;

const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS  = 1e-10;

ll to_ll(string str) {
	stringstream ss(str);
	ll res;
	ss >> res;
	return res;
}

int n;
vector<string> a[2];
vector<ll> intpart[2], decpart[2];
ll sumint[2], sumdec[2];

bool msign;
ll ansint, ansdec;

int main() {
	cin >> n;
	
	REP(i, n) {
		string str;
		cin >> str;
		if (str[0] == '-') a[1].push_back(str.substr(1));
		else a[0].push_back(str);
	}
	
	ll ten[11];
	ten[0] = 1;
	REP(i, 10) ten[i + 1] = ten[i] * 10;
	
	REP(i, 2) REP(j, a[i].size()) {
		ll nowint = 0, nowdec = 0;
		
		if (a[i][j].find(".") == string::npos)
			nowint = to_ll(a[i][j]);
		else {
			nowint = to_ll(a[i][j].substr(0, a[i][j].find(".")));
			
			string tmp = a[i][j].substr(a[i][j].find(".") + 1);
			nowdec = to_ll(tmp) * ten[10 - tmp.size()];
		}
		
		sumint[i] += nowint;
		sumdec[i] += nowdec;
		
		ll inc = sumdec[i] / 10000000000ll;
		
		sumint[i] += inc;
		sumdec[i] -= inc * 10000000000ll;
	}
	
	if (sumint[0] == sumint[1]) {
		ansint = 0;
		
		if (sumdec[0] < sumdec[1]) msign = true;
		ansdec = abs(sumdec[0] - sumdec[1]);
	}
	else if (sumint[0] > sumint[1]) {
		ansint = sumint[0] - sumint[1];
		
		if (sumdec[0] < sumdec[1]) {
			ansint--;
			ansdec = 10000000000ll - sumdec[1] + sumdec[0];
		}
		else ansdec = sumdec[0] - sumdec[1];
	}
	else {
		msign = true;
		ansint = sumint[1] - sumint[0];
		
		if (sumdec[0] > sumdec[1]) {
			ansint--;
			ansdec = 10000000000ll - sumdec[0] + sumdec[1];
		}
		else ansdec = sumdec[1] - sumdec[0];
	}
	
	printf("%s%lld.%010lld\n", msign ? "-" : "", ansint, ansdec);
	
	return 0;
}

/*
2
0.1
-1
*/
0