SZU:B47 Big Integer II

时间:2021-03-08 05:05:28

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 SZU:B47 Big Integer II, the number of test cases. There is only line for each test case. It contains two integers SZU:B47 Big Integer II.

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了。

 #include <stdio.h>
#include <string.h> char A[];
char B[];
int C[++]; char *revstr(char *str, int len){
char *start = str;
char *end = str + len - ;
char ch;
if(str != ){
while(start < end){
ch = *start;
*start ++ = *end;
*end-- = ch;
}
}
return str;
} int main(int argc, char const *argv[])
{
int t, i, j, lenA, lenB, len, Amark, Bmark, mark;
scanf("%d", &t);
while(t--){ mark=;
Amark = ;
Bmark = ; memset(A, '', sizeof(A));
memset(B, '', sizeof(B));
memset(C, , sizeof(C));
scanf("%s", A);
scanf("%s", B);
lenA = strlen(A);
lenB = strlen(B); if(A[] == '-'){
for(i=;i<lenA;++i)
A[i-] = A[i];
A[i-]= '\0';
A[i] = '';
Amark = ;
lenA--;
} if(B[] == '-'){
for(i=;i<lenB;++i)
B[i-] = B[i];
B[i-] = '\0';
B[i] = '';
Bmark = ;
lenB--;
}
revstr(A,lenA);
revstr(B,lenB);
A[lenA] = '\0';
B[lenB] = '\0'; for(i=;i<lenB;++i){
for(j=;j<lenA;++j){
C[j+i]+=(B[i]-'')*(A[j]-'');
}
}
len = lenA + lenB;
for(i=;i<len;++i){
if(C[i] > ){
C[i+]+=C[i]/;
C[i] %= ;
}
}
while(!C[len-]){
len--;
}
//printf("len = %d\n", len);
if(len == ){ printf("0\n"); continue;}
if(len > - && Amark != Bmark)
putchar('-');
for(i=len-;i>=;i--)
printf("%d", C[i]); printf("\n");
} return ;
}