結果

問題 No.81 すべて足すだけの簡単なお仕事です。
ユーザー snrnsidysnrnsidy
提出日時 2021-06-10 04:28:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,644 bytes
コンパイル時間 1,399 ms
コンパイル使用メモリ 160,804 KB
最終ジャッジ日時 2024-11-15 01:26:56
合計ジャッジ時間 1,815 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:77:28: error: call of overloaded 'abs(__int128&)' is ambiguous
   77 |         __int128 temp = abs(sum);
      |                         ~~~^~~~~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/std_abs.h:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/cmath:47,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:41,
                 from main.cpp:1:
/usr/include/stdlib.h:848:12: note: candidate: 'int abs(int)'
  848 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~

ソースコード

diff #

#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