zoukankan      html  css  js  c++  java
  • 大数运算

    大数加法

    http://acm.hdu.edu.cn/showproblem.php?pid=1002

    先一轮加上再一轮进位

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 char s1[1010], s2[1010];
     6 int a1[1010], a2[1010], ans[1010];
     7 int main()
     8 {
     9     int kase=1, t;
    10     cin >> t;
    11     while(t--){
    12         cout << "Case " << kase++ << ":" << endl;
    13         memset(a1, 0, sizeof(a1));
    14         memset(a2, 0, sizeof(a2));
    15         memset(ans, 0, sizeof(ans));
    16         cin >> s1 >> s2;
    17         cout << s1 << " + " << s2 << " = ";
    18         int l1 = strlen(s1);
    19         int l2 = strlen(s2);
    20         for(int i = l1-1; i >= 0; i--){
    21             a1[l1-i-1] = s1[i]-'0';
    22         }        
    23         for(int i = l2-1; i >= 0; i--){
    24             a2[l2-i-1] = s2[i]-'0';
    25         }
    26         int len = max(l1, l2);
    27         for(int i = 0; i < len; i++){
    28             ans[i] = a1[i]+a2[i];
    29         }    
    30         for(int i = 0; i < len; i++){
    31             if(ans[i] > 9){
    32                 ans[i+1] += ans[i]/10;
    33                 ans[i] %= 10;
    34             } 
    35         }
    36         int flag =0;
    37         for(int i = len+1; i >= 0; i--){
    38             if(!ans[i]&&!flag) ;
    39             else{
    40                 cout << ans[i];
    41                 flag=1;
    42             }
    43         }
    44         if(!flag) cout << "0";
    45         cout << endl;
    46         if(t) cout << endl;
    47     }
    48     return 0;
    49 }

    大数减法

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

    函数里实现的是:被减数大于减数的情况。

    分别判断:如果被减数长度大于减数,直接调用;如果被减数长度小于减数,多输出一个“-”;如果长度相等strcmp后用大的减小的,看情况加不加负号。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 int ans[410];
     6 void sub(int a1[], int a2[], int l1, int l2)
     7 {
     8     int len = l1;
     9     for(int i = 0; i < len; i++){
    10         if(a1[i]<a2[i]){
    11             a1[i+1]--;
    12             ans[i] = a1[i]-a2[i]+10;
    13         }
    14         else{
    15             ans[i] = a1[i]-a2[i];
    16         }
    17     } 
    18     int flag=0;
    19     for(int i = len-1; i >= 0; i--){
    20         if(!ans[i]&&!flag) ;
    21         else{
    22             cout << ans[i];
    23             flag = 1;
    24         } 
    25     }
    26     if(!flag) cout << "0";
    27     cout << endl;
    28 }
    29 int main()
    30 {
    31     char s1[210], s2[210];
    32     int a1[210], a2[210];
    33     while(cin >> s1 >> s2){
    34         //一定要memset,位数短的高位默认为0 
    35         memset(a1, 0, sizeof(a1));
    36         memset(a2, 0, sizeof(a2));
    37         memset(ans, 0, sizeof(ans));
    38         int l1 = strlen(s1);
    39         int l2 = strlen(s2);
    40         for(int i = l1-1; i >= 0; i--){
    41             a1[l1-i-1] = s1[i]-'0';
    42         }
    43         for(int i = l2-1; i >= 0; i--){
    44             a2[l2-i-1] = s2[i]-'0';
    45         }
    46         if(l1>l2)
    47             sub(a1, a2, l1, l2);
    48         else if(l1 == l2){//如果减数和被减数长度相等 
    49             int k = strcmp(s1, s2); 
    50             if(k == 0) cout << "0" << endl;
    51             else if(k > 0) sub(a1, a2, l1, l2);
    52             else{
    53                 cout << "-";
    54                 sub(a2, a1, l2, l1);
    55             }
    56         } 
    57         else{
    58             cout << "-";
    59             sub(a2, a1, l2, l1);
    60         }
    61     }
    62     return 0;
    63 }

    大数乘法

    被乘数i位*乘数的j位是结果的i+j位

    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];//被乘数i位*乘数的j位是结果的i+j位 
    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 } 
  • 相关阅读:
    1.文件I/O
    sqlite-按日期分组,根据日期查询详细内容
    sqlite-在数据库中创建默认时间
    Git-git 忽略 IntelliJ .idea文件
    重启猫(modem)的方法
    从TP、FP、TN、FN到ROC曲线、miss rate、行人检测评估
    畅所欲言第1期
    使用属性表:VS2013上配置OpenCV
    关于OOM那些事儿
    深度学习之江湖~那些大神们
  • 原文地址:https://www.cnblogs.com/Surprisezang/p/8970480.html
Copyright © 2011-2022 走看看