結果

問題 No.81 すべて足すだけの簡単なお仕事です。
コンテスト
ユーザー snrnsidy
提出日時 2021-06-10 04:28:52
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,644 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,038 ms
コンパイル使用メモリ 169,948 KB
最終ジャッジ日時 2026-05-10 07:01:37
合計ジャッジ時間 1,707 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:77:28: error: call of overloaded 'abs(__int128&)' is ambiguous
   77 |         __int128 temp = abs(sum);
      |                         ~~~^~~~~
main.cpp:77:28: note: there are 9 candidates
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/cstdlib:83,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:41,
                 from main.cpp:1:
/usr/include/stdlib.h:980:12: note: candidate 1: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/cstdlib:87:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/std_abs.h:85:3: note: candidate 2: 'constexpr long double std::abs(long double)'
   85 |   abs(long double __x)
      |   ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/std_abs.h:81:3: note: candidate 3: 'constexpr float std::abs(float)'
   81 |   abs(float __x)
      |   ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/std_abs.h:77:3: note: candidate 4: 'constexpr double std::abs(double)'
   77 |   abs(double __x)
      |   ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/std_abs.h:67:3: note: candidate 5: 'long long int std::abs(long long int)'
   67 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/std_abs.h:62:3: note: candidate 6: 'long int std::abs(long int)'
   62 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

std::ostream&
operator<<( std::ostream& dest, __int128_t value )
{
    std::ostream::sentry s( dest );
    if ( s ) {
        __uint128_t tmp = value < 0 ? -value : value;
        char buffer[ 128 ];
        char* d = std::end( buffer );
        do
        {
            -- d;
            *d = "0123456789"[ tmp % 10 ];
            tmp /= 10;
        } while ( tmp != 0 );
        if ( value < 0 ) {
            -- d;
            *d = '-';
        }
        int len = std::end( buffer ) - d;
        if ( dest.rdbuf()->sputn( d, len ) != len ) {
            dest.setstate( std::ios_base::badbit );
        }
    }
    return dest;
}

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	__int128 sum = 0;
	int n;
	string s;
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		cin >> s;
		string a,b;
		for(int j=0;j<s.length();j++)
		{
			if(s[j]=='.')
			{
				a = s.substr(0,j);
				b = s.substr(j+1);
				break;
			}
		}
		if(a=="") a = s;
		while(b.length()<10)
		{
			b += '0';
		}
		a += b;
		__int128 temp = 0;
		__int128 num = 1;
		for(int j=a.length()-1;j>=0;j--)
		{
			if(a[j]=='-') continue;
			temp += ((a[j]-'0')*num);
			num*=10;
		}
		if(a[0]=='-')
		{
			temp*=-1;
		}
		sum += temp;
	}


	string res;
	__int128 temp = abs(sum);
	while(temp > 0)
	{
		res = (char)(temp%10 + '0') + res;
		temp/=10;
		if(res.length()==10)
		{
			res = '.' + res;
		}
	}
	
	if(res.length()<10)
	{
		while(res.length()<10)
		{
			res = '0' + res;
		}
		res = "0." + res;
	}
	
	if(res[0]=='.')
	{
		res = '0' + res;
	}
		
	if(sum < 0)
	{
		res = '-' + res;
	}
	
	cout << res << '\n';
	return 0;
}
0