1065. A+B and C (64bit) (20)
时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HOU, Qiming
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).
Sample Input:3 1 2 3 2 3 4 9223372036854775807 -9223372036854775808 0Sample Output:
Case #1: false Case #2: true Case #3: false
方法二:溢出判断(参照算法笔记P22)
方法三:long double(参照别人)
方法一:
#include<stdio.h>
#include<string.h>
char s1[1000],s2[1000],s3[1000];
struct bign{
int flag;//0 正数
int len;
int num[1000];
};
void init(bign &a){
a.len=0;
a.flag=0;
for(int i=0;i<1000;i++){
a.num[i]=0;
}
}
bign change(char s[]){
bign a;
init(a);
a.len=strlen(s);
if(s[0]=='-'){
a.flag=1;
a.len--;
}
int i;
for(i=0;i<a.len;i++){
if(a.flag){
a.num[i]=s[a.len-i]-'0';//
}
else{
a.num[i]=s[a.len-1-i]-'0';//
}
}
return a;
}
bign add(bign a,bign b){
bign c;
init(c);
if(a.flag==1){//两个负数相加仍为负数
c.flag=1;
}
int i,carry=0;
for(i=0;i<a.len||i<b.len;i++){
int temp=carry+a.num[i]+b.num[i];
c.num[c.len++]=temp%10;
carry=temp/10;
}
if(carry){
c.num[c.len++]=carry;
}
return c;
}
int cmp1(bign a,bign b){//返回1,绝对值a>b
if(a.len>b.len){
return 1;
}
else if(a.len<b.len){
return 0;
}
else{
for(int i=a.len-1;i>=0;i--){
if(a.num[i]>b.num[i]){
return 1;
}
else if(a.num[i]<b.num[i]) {
return 0;
}
}
return 0;
}
}
bign sub(bign a,bign b){//绝对值 大减小
bign c;
init(c);
int i;
for(i=0;i<a.len||i<b.len;i++){
int temp=a.num[i]-b.num[i];
if(temp<0){
temp=temp+10;
a.num[i+1]--;//....i++..
}
c.num[c.len++]=temp;
}
if(a.flag&&cmp1(a,b)){//a为负数并且 绝对值 a>b
c.flag=1;
}
return c;
}
int cmp2(bign a,bign b){
int ans=0;
if(a.flag==0&&a.flag==0){//都为正数
if(cmp1(a,b)==0){//a<=b
ans=1;
}
}
else if(a.flag==1&&b.flag==0){//a负 b正
ans=1;
}
else if(a.flag==1&&b.flag==1){//都为负数
if(cmp1(a,b)==1){//绝对值 a>b
ans=1;//...
}
}
return ans;
}
int main(){
int T,i;
scanf("%d",&T);
int cou=0;
while(T--){
cou++;
scanf("%s %s %s",s1,s2,s3);
bign a=change(s1);
bign b=change(s2);
bign c=change(s3);
bign d;
if((a.flag==b.flag)){
d=add(a,b);
}
else{
if(cmp1(a,b)==0){//不看符号位,绝对值a<=b
bign temp=b;
b=a;
a=temp;
}
d=sub(a,b);
}
int ans=cmp2(d,c);
printf("Case #%d: ",cou);
if(ans==0){
printf("true\n");
}
else{
printf("false\n");
}
}
}
方法二:
#include<stdio.h>
int main(){
long long n,a,b,c,i,ans;
scanf("%lld",&n);
for(i=1;i<=n;i++){
scanf("%lld %lld %lld",&a,&b,&c);
ans=a+b;
if(a>0&&b>0&&ans<0){
printf("Case #%d: true\n",i);
}
else if(a<0&&b<0&&ans>=0){ //ans>=0
printf("Case #%d: false\n",i);
}
else if(ans>c){
printf("Case #%d: true\n",i);
}
else {
printf("Case #%d: false\n",i);
}
}
}
方法三:
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main() {
int n,count=1;
cin>>n;
long double a,b,c;
while(n--) {
cin>>a>>b>>c;
bool ans = a+b >c;
printf("Case #%d: ",count++);
printf(ans?"true\n":"false\n");
}
return 0;
}