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:
1 2 3 4 |
3 1 2 3 2 3 4 9223372036854775807 -9223372036854775808 0 |
Sample Output:
1 2 3 |
Case #1: false Case #2: true Case #3: false |
-9223372036854775808 超出了 64 bit 整数的范围……
高精度加减法即可……
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
#include<stdio.h> #include<string.h> typedef struct integer_ { int sign; char digit[22]; } integer; void reverse(char *str) { int i,n; char temp; n=strlen(str); for(i=0;i<n/2;i++) { temp=str[i]; str[i]=str[n-i-1]; str[n-i-1]=temp; } return ; } void add(integer *a,integer *b) { char *pa=a->digit,*pb=b->digit; int carry=0; reverse(a->digit); reverse(b->digit); while(*pa&&*pb) { if(*pa+*pb+carry-'0'>'9') { *pa+=*pb+carry-10-'0'; carry=1; } else { *pa+=*pb+carry-'0'; carry=0; } pa++; pb++; } if(*pb) { strcpy(pa,pb); } while(*pa) { if(*pa+carry>'9') { *pa+=carry-10; carry=1; } else { *pa+=carry; carry=0; break; } pa++; } if(carry) { *(pa++)='1'; *pa=0; } reverse(a->digit); return ; } void sub(integer *a,integer *b) { char *pa,*pb,*temp; int carry=0,l_a,l_b,flag=0; l_a=strlen(a->digit); l_b=strlen(b->digit); pa=a->digit; pb=b->digit; if(l_a==l_b&&strcmp(a->digit,b->digit)<0||l_a<l_b) { pa=b->digit; pb=a->digit; flag=1; } if(flag) { a->sign=b->sign; } reverse(a->digit); reverse(b->digit); while(*pa&&*pb) { if(*pa-*pb+carry<0) { *pa+=-*pb+carry+10+'0'; carry=-1; } else { *pa+=-*pb+carry+'0'; carry=0; } pa++; pb++; } if(*pb) { strcpy(pa,pb); } while(*pa) { if(*pa+carry<'0') { *pa+=carry+10; carry=-1; } else { *pa+=carry; carry=0; } pa++; } pa--; while(*pa=='0') { *pa=0; pa--; } if(flag) { strcpy(a->digit,b->digit); } reverse(a->digit); return ; } int compare(integer *a,integer *b) { int l_a,l_b; if(a->sign>b->sign) { return 1; } l_a=strlen(a->digit); l_b=strlen(b->digit); if(a->sign+b->sign>0) { if(l_a==l_b) { return strcmp(a->digit,b->digit); } return l_a-l_b; } if(l_a==l_b) { return -strcmp(a->digit,b->digit); } return l_b-l_a; } int main() { int i,j,n; char buf[3][21]; integer number[3]; scanf("%d",&n); for(i=0;i<n;i++) { for(j=0;j<3;j++) { scanf("%s",buf[j]); if(buf[j][0]=='-') { number[j].sign=-1; strcpy(number[j].digit,buf[j]+1); } else { number[j].sign=1; strcpy(number[j].digit,buf[j]); } } if(number[0].sign+number[1].sign==0) { sub(number,number+1); } else { add(number,number+1); } if(compare(number,number+2)>0) { printf("Case #%d: true\n",i+1); } else { printf("Case #%d: false\n",i+1); } } } |