Judge Info
- Memory Limit: 32768KB
- Case Time Limit: 10000MS
- Time Limit: 10000MS
- Judger: Normal
Description
Please calculate the answer of A+B and the answer of A-B, both A and B are integer.
Input
The first line of input contains , the number of test cases. There is only line for each test case. It contains two integers .
Output
For each test case, output two lines A+B and A-B.
Sample Input
3 1 2 1 1 -1 -1
Sample Output
3 -1 2 0 -2 0
题解:
1. 调试了很多遍,WA,之后朋友用python 测试很多大数据,我都测试了,数据正常,但还是WA,问了cxc,才发现,擦擦,尼玛,爆粗有木有? 调代码调了四个多小时只是没考虑前导0 ?带前导0也算神马整数 , -000001 是 -1 0000002 是 2 , 哪有这么无语的数据,擦擦!!
2. 第二次调试发现前导0去掉了。但是忽略了一个重要的数据,妹的 -00000 减去 +0000000 等于 -0 的有木有? 又单独考虑0的符号问题,最终AC。
虽然我代码写的很啰嗦,也很难懂,给自己做个留念吧。
1 #include <stdio.h> 2 #include <string.h> 3 4 #define max(a, b) ((a) >= (b) ? (a) : (b)) 5 6 char A[1012]; 7 char B[1012]; 8 int C[1012]; 9 int mark; 10 11 12 char *revstr(char *str, size_t len) 13 { 14 15 char *start = str; 16 char *end = str + len - 1; 17 char ch; 18 19 if (str != NULL) 20 { 21 while (start < end) 22 { 23 ch = *start; 24 *start++ = *end; 25 *end-- = ch; 26 } 27 } 28 return str; 29 } 30 31 void add(char A[], char B[]){ 32 33 int i, lenA, lenB, len; 34 memset(C,0,sizeof(C)); 35 36 lenA = strlen(A); 37 lenB = strlen(B); 38 len = max(lenA, lenB); 39 40 revstr(A,lenA); 41 revstr(B,lenB); 42 43 44 A[lenA] = '0'; 45 B[lenB] = '0'; 46 47 for(i=0;i<len+1;i++){ 48 49 C[i] = (A[i] - '0') + (B[i] - '0') ; 50 } 51 52 for(i=0;i<len+1;i++){ 53 if(C[i] > 9 ){ 54 C[i]%=10; 55 C[i+1]++; 56 } 57 } 58 59 if(mark == 1) putchar('-'); 60 if(C[len]==0){ 61 for(i=len-1;i>=0;--i) 62 printf("%d", C[i]); 63 printf(" "); 64 } 65 66 else { 67 for(i=len;i>=0;--i) 68 printf("%d", C[i]); 69 printf(" "); 70 } 71 A[lenA] = '