1. 程式人生 > >湖南大學第十四屆ACM程式設計大賽 G a+b+c+d=?

湖南大學第十四屆ACM程式設計大賽 G a+b+c+d=?

連結:https://ac.nowcoder.com/acm/contest/338/G
來源:牛客網

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld

題目描述

This is a very simple problem! Your only job is to calculate a + b + c + d!

輸入描述:

There are several cases.

In the first line, there is a single integer T.(T <= 200)

In the next T lines, each line contains four integers a, b, c and d(-2^61 <= a,b,c,d <=2^61)

輸出描述:

output T lines.

Each line output one integer represent the answer of a + b + c + d

示例1

輸入

複製

1
1 2 3 4

輸出

複製

10

分析:考察對於長整型資料範圍的認知,直接加法是會出現錯誤的,需要在a,b,c,d都等於2^61時特判直接輸出2^63,其他情況直接輸出結果即可

#include<iostream>
using namespace std;
int main()
{
	int c;cin>>n;
	while(n--)
	{
		long long a,b,c,d;
		cin>>a>>b>>c>>d;
		if(a==2305843009213693952&&b==2305843009213693952&&c==2305843009213693952&&d==2305843009213693952)
		cout<<"9223372036854775808"<<endl;
		else
		cout<<a+b+c+d<<endl;
	}
 } 

當然也可以用Python來解此題(一開始我以為是大數模擬,後來發現用Python就行了)

T=int (raw_input().strip())
for case in range(T):
    a,b,c,d=map(int,raw_input().strip().split())
    print a+b+c+d