結果

問題 No.81 すべて足すだけの簡単なお仕事です。
ユーザー data9824data9824
提出日時 2015-06-03 23:28:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,665 bytes
コンパイル時間 579 ms
コンパイル使用メモリ 68,340 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-29 22:29:38
合計ジャッジ時間 1,841 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <cstdlib>
#include <iomanip>
#include <stdexcept>
#include <cassert>

using namespace std;

const long long SCALE = 10000000000LL;

class FixedPoint {
public:
	long long sign;
	long long integer;
	long long decimal;
	FixedPoint() : sign(1), integer(0), decimal(0) {}
	FixedPoint(long long sign, long long integer, long long decimal) : 
		sign(sign), integer(integer), decimal(decimal) {}
	FixedPoint(const string& s) : sign(1), integer(0), decimal(0) {
		enum {
			STATE_SIGN,
			STATE_INTEGER,
			STATE_DECIMAL
		} state = STATE_SIGN;
		long long digit = SCALE / 10;
		for (size_t i = 0; i < s.size(); ++i) {
			char ch = s[i];
			switch (state)
			{
			case STATE_SIGN:
				if (ch == '+') {
					state = STATE_INTEGER;
				} else if (ch == '-') {
					sign = -1;
					state = STATE_INTEGER;
				} else if (ch == '.') {
					state = STATE_DECIMAL;
				} else if (isdigit(ch)) {
					integer = ch - '0';
					state = STATE_INTEGER;
				} else {
					throw exception();
				}
				break;
			case STATE_INTEGER:
				if (ch == '.') {
					state = STATE_DECIMAL;
				} else if (isdigit(ch)) {
					integer *= 10LL;
					integer += ch - '0';
				} else {
					throw exception();
				}
				break;
			case STATE_DECIMAL:
				if (isdigit(ch)) {
					decimal += (long long)(ch - '0') * digit;
					digit /= 10;
				} else {
					throw exception();
				}
				break;
			}
		}
	}
	FixedPoint abs() const {
		return FixedPoint(1, integer, decimal);
	}
	FixedPoint negate() const {
		if (integer == 0 && decimal == 0) {
			return *this;
		}
		return FixedPoint(sign * -1, integer, decimal);
	}
	static FixedPoint add(const FixedPoint& lhs, const FixedPoint& rhs) {
		assert(lhs.sign > 0 && rhs.sign);
		long long decimal = lhs.decimal + rhs.decimal;
		long long integer = lhs.integer + rhs.integer + decimal / SCALE;
		return FixedPoint(1, integer, decimal % SCALE);
	}
	static FixedPoint subtract(const FixedPoint& lhs, const FixedPoint& rhs) {
		assert(lhs.sign > 0 && rhs.sign);
		// assert(lhs >= rhs);
		long long decimal;
		long long integer;
		if (lhs.decimal < rhs.decimal) {
			decimal = SCALE + lhs.decimal - rhs.decimal;
			integer = lhs.integer - rhs.integer - 1;
		} else {
			decimal = lhs.decimal - rhs.decimal;
			integer = lhs.integer - rhs.integer;
		}
		return FixedPoint(1, integer, decimal);
	}
};
bool operator<(const FixedPoint& lhs, const FixedPoint& rhs) {
	if (lhs.sign != rhs.sign) {
		return (lhs.sign < rhs.sign);
	}
	if (lhs.integer != rhs.integer) {
		return (lhs.integer * lhs.sign < rhs.integer * rhs.sign);
	}
	return (lhs.decimal * lhs.sign < rhs.decimal * rhs.sign);
}
FixedPoint operator+(const FixedPoint& lhs, const FixedPoint& rhs) {
	FixedPoint large, small;
	if (lhs.abs() < rhs.abs()) {
		small = lhs;
		large = rhs;
	} else {
		small = rhs;
		large = lhs;
	}
	if (large.sign > 0 && small.sign > 0) {
		return FixedPoint::add(large, small);
	} else if (large.sign > 0 && small.sign < 0) {
		return FixedPoint::subtract(large, small.negate());
	} else if (large.sign < 0 && small.sign > 0) {
		return FixedPoint::subtract(large.negate(), small).negate();
	} else {
		return FixedPoint::add(large.negate(), small.negate()).negate();
	}
}
ostream& operator<<(ostream& lhs, const FixedPoint& rhs) {
	lhs << (rhs.sign < 0 ? "-" : "") << rhs.integer << "." << setfill('0') << setw(10) << rhs.decimal;
	return lhs;
}

int main() {
	int n;
	cin >> n;
	vector<FixedPoint> a;
	for (int i = 0; i < n; ++i) {
		string s;
		cin >> s;
		a.push_back(FixedPoint(s));
	}
	FixedPoint sum = accumulate(a.begin(), a.end(), FixedPoint());
	cout << sum << endl;
	return 0;
}
0