浙大机试2013:A+B and C (64bit) (20)

来源:互联网 发布:js捕获事件target 编辑:程序博客网 时间:2024/05/29 17:26

http://www.patest.cn/contests/pat-a-practise/1065


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:
31 2 32 3 49223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: falseCase #2: trueCase #3: false

费了老半天的劲写了一堆渣代码出来,然后一看别人的解法,顿时就蒙了。为什么这么简单!!!!

虽然代码写得烂,贴出来留作纪念吧。自我安慰一下吧,我的算法可以处理任意位数的数据,只需要把数组大小改一下就可以了。


#include <stdio.h>#include <string.h>#define max(a,b) ((a)>(b)?(a):(b))char A[63];char B[63];char C[63];int nA[15];int nB[15];int nC[15];int nsum[15];int len[3];void init(){int i;for(i = 0;i < 15;i++)nA[i] = nB[i] = nC[i] = 0;}int trans(char A[],int n[]){int l = strlen(A);int i = l,j,tmp_i;int sum = 0,cnt = 0;int end = 0;if(A[0] == '-') end = 1;tmp_i = i;i = i - 5;while(i >= end){for(j = 0;j < 5;j++){sum = sum * 10 + A[i+j] + (0 - '0');}tmp_i = i;i = i - 5;n[cnt++] = sum;sum = 0;}if(i < end){i = end;for(j = 0;i + j < tmp_i;j++)sum = sum * 10 + A[i+j] + (0 - '0');n[cnt++] = sum;}return cnt;}int main(){int T,i,j,k;int case_n;scanf("%d",&T);for(i = 1;i <= T;i++){init();scanf("%s %s %s",A,B,C);len[0] = trans(A,nA);len[1] = trans(B,nB);len[2] = trans(C,nC);if(A[0] != '-' && B[0] != '-') case_n = 0;else if(A[0] != '-' && B[0] == '-') case_n = 1;else if(A[0] == '-' && B[0] != '-') case_n = 2;else if(A[0] == '-' && B[0] == '-') case_n = 3;j = max(len[0],len[1]);k = 0;switch(case_n){case 0:while(k < j){nsum[k] = nA[k] + nB[k];k++;}break;case 1:while(k < j){nsum[k] = nA[k] - nB[k];k++;}break;case 2:while(k < j){nsum[k] = nB[k] - nA[k];k++;}break;case 3:while(k < j){nsum[k] = -(nA[k] + nB[k]);k++;}break;}for(k = 0;k < j;k++)nA[k] = 0;if(C[0] == '-'){j = max(j,len[2]);k = 0;while(k < j){nA[k] = nsum[k] + nC[k];k++;}}else{j = max(j,len[2]);k = 0;while(k < j){nA[k] = nsum[k] - nC[k];k++;}}for(k = 0;k < j - 1;k++){while(nA[k] < 0){nA[k+1]--;nA[k] = nA[k] + 100000;}while(nA[k] >= 100000){nA[k+1]++;nA[k] = nA[k] - 100000;}}k = j - 1;while(k >= 0){if(nA[k] == 0)k--;else break;}if( k >= 0 && nA[k] > 0)printf("Case #%d: true\n",i);elseprintf("Case #%d: false\n",i);}return 0;}


0 0
原创粉丝点击