不说废话先上代码
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll a,b,c;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
bool flag;
scanf("%lld %lld %lld",&a,&b,&c);
ll res=a+b;
if(res<0&&a>0&&b>0)flag=1;
else if(res>=0&&a<0&&b<0)flag=0;
else if(res>c)flag=1;
else flag=0;
cout<<"Case #"<<i<<": ";
if(flag==1)cout<<"true"<<endl;
else cout<<"false"<<endl;
}
}
解题思路
法一 考虑溢出的判断
首先要清楚 long long 的范围【-2^63,2^63)因此单独的a,b不会超出 ll ,但相加后有可能溢出。
当A+B>=2^63时,显然有A+B>C成立 此时A+B值的区间为【-2^63,-2】 ,同理考虑负溢出的情况。
若不理解可简单的记成(原理位进位到了符号位)
两个正数之和相加等于负数表示发生了正溢出
两个负数之和相加等于正数表示发生了负溢出
注:对于longlong的读取谨慎使用cin(经个人测试时用所给数据的最大范围仍可以读取)但在PAT平台上会发生错误
emmmm大家知道为什么cin在PAT上过不了的话可以教教我
法二 以字符串的形式读取,采用高精度加法
附高精度模板
// C = A + B, A >= 0, B >= 0
vector<int> add(vector<int> &A, vector<int> &B)
{
if (A.size() < B.size()) return add(B, A);
vector<int> C;
int t = 0;
for (int i = 0; i < A.size(); i ++ )
{
t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if (t) C.push_back(t);
return C;
}
题目描述 原题传送门
Given three integers A, B and C in (−263,263), you are supposed to tell whether A+B>C.
Input Specification:
The first line of the input gives the positive number of test cases, T (≤10). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.
Output Specification:
For each test case, output in one line Case #X: true
if A+B>C, or Case #X: false
otherwise, where X is the case number (starting from 1). Each line should ends with '\n'
.
Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
Thanks to Jiwen Lin for amending the test data.
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
中文释义
给定三个整数 A,B,C,请你判断 A+B>C是否成立。输入格式第一行包含整数 T,表示共有 T组测试数据。接下来 T行,每行包含一组数据,即三个整数 A,B,C。
输出格式
每行输出一个结果,如果不等式成立,输出 Case #X: true
,否则输出 Case #X: false
,其中 X
表示数据编号(从 1开始)。数据范围−263≤A,B,C≤263−1
输入样例:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
输出样例:
Case #1: false
Case #2: true
Case #3: false