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
经验教训就是不要妄图用加法搞定一切,写的各种麻烦。不如直接再写出个减法,轻松愉快。
#include<string> #include<stdio.h> #include<iostream> #include<string.h> #include<queue> #include<algorithm> #include<map> #include<set> #include<math.h> #include<stack> #include<vector> using namespace std; struct ZZ{ int num[1000]; int p=0; int mark; // 标记正负。1为正,-1为负 void getnum(char x[]) { p=0; for(int i=0;i<1000;i++) num[i]=0; int l=strlen(x)-1; int k=1; int sum=0; mark=1; if(x[0]!='-') { for(int i=l;i>=0;i--) { sum+=(x[i]-'0')*k; k*=10; if(k==10000||i==0) { k=1; num[p++]=sum; sum=0; } } } else { mark=-1; if(strlen(x)==2&&x[1]=='0') mark=1; for(int i=l;i>=1;i--) { sum+=(x[i]-'0')*k; k*=10; if(k==10000||i==1) { k=1; num[p++]=sum; sum=0; } } } } ZZ operator + (const ZZ &A) const { ZZ tmp; tmp.getnum("0"); int pp=0; int d=0; while(pp<p||pp<A.p) { int sum=num[pp]+A.num[pp]+d; tmp.num[pp++]=sum%10000; d=sum/10000; } if(d!=0) tmp.num[pp++]=d; tmp.mark=mark; tmp.p=pp; return tmp; } ZZ operator - (const ZZ &A) const { ZZ tmp; tmp.getnum("0"); int pp=0; int d=0; while(pp<p) { int sum=num[pp]-A.num[pp]-d; d=0; if(sum<0) { sum+=10000; d=1; } tmp.num[pp++]=sum; } for(int i=100;i>=0;i--) if(tmp.num[i]!=0) {pp=i+1;break; } tmp.p=pp; tmp.mark=mark; return tmp; } bool postive() //判断是否为正数 { int fla=0; for(int i=0;i<p;i++) if(num[i]!=0) { fla=1; break; } if(fla==1&&mark==1) return true; return false; } bool operator > (const ZZ &A) const //比较绝对值大小 { if(p!=A.p) return p>A.p; for(int i=p-1;i>=0;i--) if(num[i]!=A.num[i]) return num[i]>A.num[i]; return false; } }; int main() { char a[70],b[70],c[70]; int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a>>b>>c; ZZ t1,t2,t3,t4; t1.getnum(a); t2.getnum(b); t3.getnum(c); if(t1.mark==t2.mark) t4=t1+t2; else { if(t1>t2) t4=t1-t2; else t4=t2-t1; } //cout<<t4.mark<<' '<<t4.num[1]<<' '<<t4.num[0]<<' '<<t4.p<<endl; if(t4.mark!=t3.mark) t4=t4+t3; else{ if(t4>t3) t4=t4-t3; else {t4=t3-t4;t4.mark=-t3.mark;} } //cout<<t4.mark<<' '<<t4.num[1]<<' '<<t4.num[0]<<' '<<t4.p<<endl; cout<<"Case #"<<i<<": "; if(t4.postive()) cout<<"true"; else cout<<"false"; cout<<endl; } return 0; }
看了人家的方法,羞愧
下面俩仅仅是sum的差别。居然第三个过不了,神奇
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月13日 11:26 | 答案正确 | 20 | 1065 | C++ (g++ 4.7.2) | 1 | 384 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 308 | 12/12 |
1 | 答案正确 | 1 | 384 | 4/4 |
2 | 答案正确 | 1 | 308 | 4/4 |
#include <iostream> using namespace std; int main() { int T, index; long long a, b, c,sum; bool flag; cin >> T; for (index = 0; index < T;) { index++; cin >> a >> b >> c; sum = a + b; if (a>0 && b > 0 && sum <= 0)flag = true; else if(a < 0 && b < 0 && sum>= 0)flag = false; else if (sum> c)flag = true; else flag = false; cout << "Case #" << index << ": " << (flag ? "true" : "false" )<< endl; } system("pause"); return 0; }
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月13日 11:23 | 部分正确 | 12 | 1065 | C++ (g++ 4.7.2) | 1 | 436 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 436 | 12/12 |
1 | 答案错误 | 1 | 308 | 0/4 |
2 | 答案错误 | 1 | 436 | 0/4 |
#include <iostream> using namespace std; int main() { int T, index; long long a, b, c; bool flag; cin >> T; for (index = 0; index < T;) { index++; cin >> a >> b >> c; if (a>0 && b > 0 && (a + b) <= 0)flag = true; else if(a < 0 && b < 0 && (a + b )>= 0)flag = false; else if ((a + b )> c)flag = true; else flag = false; cout << "Case #" << index << ": " << (flag ? "true" : "false" )<< endl; } system("pause"); return 0; }
sum过了而a+b没过是因为,当数据比较大时(即a+b > 2^63-1) 此时sum因为规定为long long了,所以会被当成负数。 而a+b在2^64和2^63-1 之间会被当做无符号长整型处理。ISO C99里这么规定的