結果
| 問題 |
No.81 すべて足すだけの簡単なお仕事です。
|
| コンテスト | |
| ユーザー |
data9824
|
| 提出日時 | 2015-06-03 23:28:05 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 3,665 bytes |
| コンパイル時間 | 583 ms |
| コンパイル使用メモリ | 69,956 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-22 16:29:26 |
| 合計ジャッジ時間 | 1,395 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
#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;
}
data9824