結果

問題 No.81 すべて足すだけの簡単なお仕事です。
ユーザー bal4ubal4u
提出日時 2019-04-12 06:38:40
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,759 bytes
コンパイル時間 1,386 ms
コンパイル使用メモリ 29,940 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 22:48:42
合計ジャッジ時間 1,379 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// yukicoder: No.81 すべて足すだけの簡単なお仕事です
// 2019.4.12 bal4u
// 整数部と小数部を分かて足す

#include <stdio.h>
#include <ctype.h>

#define gc() getchar()
int in()    // 非負整数の入力
{
	int n = 0, c = gc();
//	while (isspace(c)) c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (isdigit(c));
	return n;
}

int ins(char *s)  // 文字列の入力 スペース以下の文字で入力終了
{
	char *p = s;
	do *s = gc();
	while (*s++ > ' ');
	*--s = 0;
	return s - p;
}

#define B10  10000000000  // 10^10
#define ABS(x) ((x)>=0?(x):-(x))
char a[50];
long long x[102], y[102]; char minus[102];

char *num(long long *res, char *s)
{
	long long n = 0;
	while (isdigit(*s)) n = 10 * n + (*s++ & 0xf);
	*res = n;
	return s;
}

int main()
{
	int i, w, N;
	char *p;
	long long sx, sy;

	N = in();
	for (i = 0; i < N; i++) {
		// 整数部と小数部に分ける。小数部を正確に10桁に揃う
		ins(p = a);
		if (*p == '-') minus[i] = 1, p++;
		p = num(x + i, p);
		if (*p != '.') y[i] = 0;
		else {
			w = num(y + i, p + 1) - p - 1;
			while (w < 10) w++, y[i] *= 10;  // 桁数を10に揃える
		}
	}

	// 整数部と小数部を別々に加算
	sx = sy = 0;
	for (i = 0; i < N; i++) {
		if (minus[i]) sx -= x[i], sy -= y[i];
		else          sx += x[i], sy += y[i];
	}

	// 桁上げと符号処理
	if (sy >= 0) sx += sy / B10, sy %= B10;
	else         sx -= (-sy) / B10, sy = -((-sy) % B10);

	if (sx == 0) goto frac;
	if (sy != 0) {
		if (sx < 0 && sy > 0) sx++, sy = sy - B10;
		else if (sx >= 0 && sy < 0) sx--, sy = B10 + sy;
	}
	if (sx == 0) {
frac:	if (sy >= 0) printf("0.%010lld\n", sy);
		if (sy < 0) printf("-0.%010lld\n", -sy);
	} else printf("%lld.%010lld\n", sx, ABS(sy));
	return 0;
}
0