結果

問題 No.81 すべて足すだけの簡単なお仕事です。
ユーザー mogurockermogurocker
提出日時 2017-10-18 00:01:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,861 bytes
コンパイル時間 1,433 ms
コンパイル使用メモリ 155,008 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 22:41:59
合計ジャッジ時間 2,537 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for(int i = 0; i < (n); i++)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())
typedef long long ll;
typedef pair<int, int> P;

int n;

string f(string s) {
	int pos = -1;
	FOR(i, s.size()) {
		if (s[i] == '.') pos = i;
	}
	string t;
	if (pos == -1) t = string(11-(int)s.size(), '0') + s + string(10, '0');
	else t = string(11-pos, '0') + s.substr(0, pos) + s.substr(pos+1) + string(10-(s.size()-pos-1), '0');
	reverse(ALL(t));
	return t;
}

string calc(string s, string t, int type) {
	string ret = string(21, '0');
	if (type == 0) {
		int c = 0;
		FOR(i, 21) {
			int a = s[i]-'0', b = t[i]-'0', res = a+b+c;
			c = res/10;
			ret[i] = (char)('0'+(res%10));
		}
	}
	else {
		reverse(ALL(s));
		reverse(ALL(t));
		bool minus = false;
		if (s < t) {
			swap(s, t);
			minus = true;
		}
		reverse(ALL(s));
		reverse(ALL(t));
		int c = 0;
		FOR(i, 21) {
			int a = s[i]-'0', b = t[i]-'0', res = a-b-c;
			if (res < 0) {
				c = 1;
				ret[i] = (char)('0'+(res+10));
			}
			else {
				c = 0;
				ret[i] = (char)('0'+res);
			}
		}
		reverse(ALL(ret));
		ret = ret.substr(0, 11) + "." + ret.substr(11);
		int p = 0;
		while (p < 20 && ret[p] == '0') p++;
		ret = ret.substr(min(10, p));
		if (minus) ret = "-" + ret;
	}
	return ret;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin >> n;
	vector<string> plus, minus;
	FOR(i, n) {
		string s;
		cin >> s;
		if (s[0] == '-') minus.push_back(f(s.substr(1)));
		else plus.push_back(f(s));
	}
	string x = string(21, '0');
	FOR(i, plus.size()) {
		string ret = calc(x, plus[i], 0);
		x = ret;
	}
	string y = string(21, '0');
	FOR(i, minus.size()) {
		string ret = calc(y, minus[i], 0);
		y = ret;
	}
	cout << calc(x, y, 1) << endl;
	return 0;
}
0