SZU:B47 Big Integer II

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 T(1 \leq T \leq 1000), the number of test cases. There is only line for each test case. It contains two integers A,B(-10^{100} \leq A,B \leq 10^{100}).

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 }

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值