Large number operations

Multiplication of large numbers

http://bailian.openjudge.cn/practice/2980/

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 char s1[210], s2[210];
 6 int a1[210], a2[210], ans[410];//数组大小要开对 
 7 int main()
 8 {
 9     while(cin >> s1 >> s2){
10         int l1 = strlen(s1);
11         int l2 = strlen(s2);
12         for(int i = l1-1; i >= 0; i--){
13             a1[l1-i-1] = s1[i]-'0';
14         }
15         for(int i = l2-1; i >= 0; i--){
16             a2[l2-i-1] = s2[i]-'0';
17         }
18         memset(ans, 0, sizeof(ans));
19         for(int i = 0 ; i < l1; i++ ){
 20              for ( int j = 0 ; j < l2; j++ ){
 21                  ans[i+j] += a1[i]*a2[j]; // multiplicand i bits *The j bits of the multiplier are the i+j bits of the result 
22              }
 23          }
 24          for ( int i = 0 ; i < l1+l2; i++ ){
 25              ans[i+ 1 ] += ans[i]/ 10 ;
 26              ans[i] %= 10 ;
 27          }
 28          int flag= 0 ;
 29         for(int i = l1+l2; i >= 0; i--){
30             if(!ans[i]&&!flag) ;
31             else {
32                 cout << ans[i];
33                 flag=1;
34             }
35         }
36         if(!flag) cout << "0";//如果结果就是0 
37         cout << endl;
38     }
39     return 0;
40 } 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325031479&siteId=291194637