A+B and C (64bit) (20)
时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
Given three integers A, B and C in [-263, 263), you are supposed to tell whether A+B > C.
输入描述:
The first line of the input gives the positive number of test cases, T (<=1000). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.
输出描述:
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).
输入例子:
3 1 2 3 2 3 4 9223372036854775807 -9223372036854775808 0
输出例子:
Case #1: false Case #2: true Case #3: false
解析:int 4字节(32比特(4*8),其中第一位是符号位) -2^31~2^31-1 ,-2147483648 ~ 2147483647 (10位十进制数)
long long 8字节(64比特(8*8) -2^63~2^63-1 ,- 9223372036854775808 ~ 9223372036854775807(20位十进制数)
float 4字节(32比特(4*8))6位有效数字
double 8字节(64比特(8*8))15位有效数字
long double 16字节(128比特(16*8))有效位至少是19位
所以该题用long double数据类型。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
double a,b,c;
cin>>a>>b>>c;
cout<<a+b<<endl;
cout<<"Case #"<<i<<": "<<(a+b>c? "true":"false")<<endl;
}
return 0;
}
方法二:考虑溢出情况(如:负19个8 + 负19个8会>0的情况,- 9223372036854775808)
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
long long a,b,c;
cin>>a>>b>>c;
long long plus = a+b;
bool flag = false;
if(a>0&&b>0&&plus<=0) flag=true;//Overflows
else if(a<0&&b<0&&plus>=0) flag=false;//Overflows
else plus>c?flag=true:flag=false;
cout<<"Case #"<<i<<": "<<(flag? "true":"false")<<endl;
}
return 0;
}