Judge Info
- Memory Limit: 32768KB
- Case Time Limit: 10000MS
- Time Limit: 10000MS
- Judger: Normal
Description
Please calculate 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 A*B in one line.
Sample Input
3 1 2 1 1 -1 -1
Sample Output
2 1 1
Ps : 算法课的小练习,通过字符串处理。注意0就OK了。
1 #include <stdio.h> 2 #include <string.h> 3 4 char A[200]; 5 char B[200]; 6 int C[200+200+1]; 7 8 char *revstr(char *str, int len){ 9 char *start = str; 10 char *end = str + len - 1; 11 char ch; 12 if(str != 0){ 13 while(start < end){ 14 ch = *start; 15 *start ++ = *end; 16 *end-- = ch; 17 } 18 } 19 return str; 20 } 21 22 int main(int argc, char const *argv[]) 23 { 24 int t, i, j, lenA, lenB, len, Amark, Bmark, mark; 25 scanf("%d", &t); 26 while(t--){ 27 28 mark=0; 29 Amark = 0; 30 Bmark = 0; 31 32 memset(A, '0', sizeof(A)); 33 memset(B, '0', sizeof(B)); 34 memset(C, 0, sizeof(C)); 35 scanf("%s", A); 36 scanf("%s", B); 37 lenA = strlen(A); 38 lenB = strlen(B); 39 40 if(A[0] == '-'){ 41 for(i=1;i<lenA;++i) 42 A[i-1] = A[i]; 43 A[i-1]= '\0'; 44 A[i] = '0'; 45 Amark = 1; 46 lenA--; 47 } 48 49 if(B[0] == '-'){ 50 for(i=1;i<lenB;++i) 51 B[i-1] = B[i]; 52 B[i-1] = '\0'; 53 B[i] = '0'; 54 Bmark = 1; 55 lenB--; 56 } 57 revstr(A,lenA); 58 revstr(B,lenB); 59 A[lenA] = '\0'; 60 B[lenB] = '\0'; 61 62 for(i=0;i<lenB;++i){ 63 for(j=0;j<lenA;++j){ 64 C[j+i]+=(B[i]-'0')*(A[j]-'0'); 65 } 66 } 67 len = lenA + lenB; 68 for(i=0;i<len;++i){ 69 if(C[i] > 9){ 70 C[i+1]+=C[i]/10; 71 C[i] %= 10; 72 } 73 } 74 while(!C[len-1]){ 75 len--; 76 } 77 //printf("len = %d\n", len); 78 if(len == 0){ printf("0\n"); continue;} 79 if(len > -1 && Amark != Bmark) 80 putchar('-'); 81 for(i=len-1;i>=0;i--) 82 printf("%d", C[i]); 83 84 printf("\n"); 85 } 86 87 return 0; 88 }