• 大整数加法减法与乘法


    模拟运算

    用数组逆向保存大数每一位 加法直接从尾向前相加(尾部已对齐) 注意进位

    减法类似 确保大数减小数 不够减了向前减一 需要注意符号的有无问题

      1 #include <iostream>
      2 #include <cstring>
      3 #include <string>
      4 using namespace std;
      5 
      6 int a[11000], b[11000];
      7 string s1, s2;
      8 
      9 void Add()
     10 {
     11     int len1 = s1.length();
     12     int len2 = s2.length();
     13     int Max = max(len1, len2);
     14     int k;
     15     for (int i = len1-1, k = 0; i >= 0; i--)
     16         a[k++] = s1[i] - '0';
     17     for (int i = len2-1, k = 0; i >= 0; i--)
     18         b[k++] = s2[i] - '0';
     19 
     20     int up = 0;
     21     for (int i = 0; i < Max+1; i++) {
     22         a[i] = a[i] + b[i] + up;
     23         up = a[i] / 10;
     24         a[i] %= 10;
     25     }
     26     int first;
     27     for (first = Max+1; first >= 0; first--)
     28         if (a[first])
     29             break;
     30 
     31     if (first == -1)
     32         cout << "0";
     33     for (int i = first; i >= 0; i--)
     34         cout << a[i];
     35 
     36 }
     37 
     38 void Sub(string Max, string Min)  //a - b
     39 {
     40     int len1 = Max.length();
     41     int len2 = Min.length();
     42     int MaxLen = max(len1, len2);
     43 
     44     int k;
     45     for (int i = len1-1, k = 0; i >= 0; i--)
     46         a[k++] = Max[i] - '0';
     47     for (int i = len2 - 1, k = 0; i >= 0; i--)
     48         b[k++] = Min[i] - '0';
     49 
     50     int up = 0;
     51     for (int i = 0; i < MaxLen+1; i++) {
     52         a[i] = a[i] - b[i] - up;
     53         if (a[i] < 0) {
     54             up = 1;
     55             a[i] += 10;
     56         }
     57         else
     58             up = 0;
     59     }
     60 
     61     int first;
     62     for (first = MaxLen+1; first >= 0; first--)
     63         if (a[first])
     64             break;
     65 
     66     if (first == -1)
     67         cout << "0";
     68     else {
     69         for (int i = first; i >= 0; i--)
     70             cout << a[i];
     71     }
     72 }
     73 
     74 int main()
     75 {
     76     cin >> s1 >> s2;
     77 
     78     memset(a, 0, sizeof(a));
     79     memset(b, 0, sizeof(b));
     80 
     81     if (s1[0] == '-' && s2[0] == '-')
     82     {
     83         cout << '-';
     84         s1.erase(0, 1);
     85         s2.erase(0, 1);
     86         Add();
     87     }
     88     else if (s1[0] == '-' || s2[0] == '-')
     89     {
     90         string Max, Min;
     91         if (s1[0] == '-') {
     92             if (s1.length()-1 > s2.length())
     93                 cout << "-";
     94             if (s1.length()-1 == s2.length())
     95             {
     96                 if (s1[1] > s2[0])
     97                     cout << "-";
     98             }
     99             s1.erase(0, 1);
    100         }
    101 
    102        if (s2[0] == '-') {
    103 
    104             if (s2.length()-1 > s1.length())
    105                 cout << "-";
    106             if (s1.length() == s2.length()-1)
    107             {
    108                 if (s2[1] > s1[0])
    109                     cout << "-";
    110             }
    111             s2.erase(0, 1);
    112         }
    113 
    114         if(s1.length() > s2.length())
    115         {
    116             Max = s1;
    117             Min = s2;
    118         }
    119         else if (s1.length() == s2.length() && s1[0] > s2[0])
    120         {
    121             Max = s1;
    122             Min = s2;
    123         }
    124         else {
    125             Min = s1;
    126             Max = s2;
    127         }
    128       Sub(Max, Min);
    129     }
    130     else
    131         Add();
    132 
    133     return 0;
    134 }

     乘法

    先乘再进位

     1 #include <iostream>
     2 #include <string>
     3 #include <cstring>
     4 using namespace std;
     5 int a[1100], b[1100], c[2300];
     6 
     7 void big_number_multipy(string s1, string s2)
     8 {
     9     int lenth1 = s1.length();
    10     int lenth2 = s2.length();
    11     int MaxLen = lenth1 + lenth2;
    12 
    13     int i, j, k;
    14     for (i = lenth1-1, k = 0; i >= 0; i--)
    15         a[k++] = s1[i] - '0';
    16     for (i = lenth2-1, k = 0; i >= 0; i--)
    17         b[k++] = s2[i] - '0';
    18 
    19     for (i = 0; i < lenth1; i++)
    20         for (j = 0; j <lenth2; j++)
    21             c[i+j] += a[i] * b[j];
    22 
    23     for (i = 0; i < MaxLen; i++) {
    24         c[i+1] += c[i] / 10;
    25         c[i] %= 10;
    26     }
    27 
    28     int first;
    29     for (first = MaxLen; first >= 0; first--)
    30         if (c[first])
    31             break;
    32     if (first == -1)
    33         cout << "0";
    34     for (int i = first; i >= 0; i--)
    35         cout << c[i];
    36 }
    37 
    38 int main()
    39 {
    40     string s1, s2;
    41 
    42     cin >> s1 >> s2;
    43     memset(a, 0, sizeof(a));
    44     memset(b, 0, sizeof(b));
    45     memset(c, 0, sizeof(c));
    46     big_number_multipy(s1, s2);
    47 
    48 
    49     return 0;
    50 }
  • 相关阅读:
    windows对象的属性和方法
    JavaScript事件处理
    HTML骨架-深入理解
    js阻止元素的默认事件与冒泡事件
    CSS3之背景色渐变
    CSS3匹配屏幕横竖状态
    LINUX系统GIT使用教程
    博客园文章页显示布局调整
    JS编码解码详解
    JS设置获取cookies
  • 原文地址:https://www.cnblogs.com/whileskies/p/6843553.html
Copyright © 2020-2023  润新知