高精度入门(减法、加法、乘法)之 CODE[VS] 3115、3116、3117

3115 高精度练习之减法:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cmath>
  5 #include <string>
  6 #include <algorithm>
  7 #include <cstring>
  8 #include <set>
  9 #include <utility>
 10 #include <locale>
 11 #include <ctime>
 12 using namespace std;
 13 //using int64 = long long;
 14 const int INF = 0x3f3f3f3f;
 15 const int MaxN = 110;
 16 
 17 string A, B;
 18 string ans;
 19 
 20 void Repair()
 21 {
 22     // 使两个字符串具有相同长度
 23     if (A.size() < B.size())
 24     {
 25         for (int i = A.size(); i < B.size(); ++i)
 26         {
 27             A = '0' + A;
 28         }
 29     }else
 30     {
 31         for (int i = B.size(); i < A.size(); ++i)
 32         {
 33             B = '0' + B;
 34         }
 35     }
 36 }
 37 
 38 void Solve()
 39 {
 40 
 41     bool isNegative = true;
 42     // 倘若A<B,输出负号,并交换A和B的值
 43     for (int i = 0; i < A.size(); ++i)
 44     {
 45         if (A[i] - '0' == B[i] - '0') continue;
 46         if (A[i]-'0' > B[i]-'0')
 47         {
 48             isNegative = false;
 49         }
 50         break;
 51     }
 52     if (isNegative) {
 53         cout << "-";
 54         swap(A, B);
 55     }
 56     // 大数减法
 57     for (int i = A.size() - 1; i >= 0; --i)
 58     {
 59         if (A[i] < B[i])
 60         {
 61             // 借位,注意所借之位为0的情况,借位之后,这些位变为9
 62             int tmp = i - 1;
 63             while ((A[tmp]-'0')-1 < 0) --tmp;
 64             A[tmp] = '0' + ((A[tmp] - '0') - 1);
 65             ++tmp;
 66             while (tmp < i)
 67             {
 68                 A[tmp] = '9';
 69                 ++tmp;
 70             }
 71             ans = static_cast<char>('0' + ((A[i] - '0' + 10) - (B[i] - '0'))) + ans;
 72         }else
 73         {
 74             // 无需借位,直接减
 75             ans = static_cast<char>('0' + (A[i] - B[i])) + ans;
 76         }
 77     } 
 78     bool Judge = false;
 79     for (int i = 0; i < ans.size(); ++i)
 80     {
 81         // 忽略前导零
 82         if (ans[i] != '0' || Judge)
 83         {
 84             cout << ans[i];
 85             Judge = true;
 86         }
 87     }
 88     cout << endl;
 89 }
 90 
 91 int main()
 92 {
 93 
 94     while (cin >> A >> B)
 95     {
 96         ans.clear();
 97         Repair();
 98         Solve();
 99     }
100 
101 #ifdef HOME
102     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
103 #endif
104     return 0;
105 }

3116 高精度练习之加法:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <string>
 6 #include <algorithm>
 7 #include <cstring>
 8 #include <set>
 9 #include <utility>
10 #include <locale>
11 #include <ctime>
12 using namespace std;
13 //using int64 = long long;
14 const int INF = 0x3f3f3f3f;
15 const int MaxN = 110;
16 
17 string A, B;
18 string ans;
19 
20 void Repair()
21 {
22     if (A.size() < B.size())
23     {
24         for (int i = A.size(); i < B.size(); ++i)
25         {
26             A = '0' + A;
27         }
28     }else
29     {
30         for (int i = B.size(); i < A.size(); ++i)
31         {
32             B = '0' + B;
33         }
34     }
35 }
36 
37 void Solve()
38 {
39     int tmp = 0;
40     for (int i = A.size() - 1; i >= 0; --i)
41     {
42         if ((A[i]-'0') + (B[i] - '0') + tmp > 9)
43         {
44             ans = static_cast<char>('0' + ((A[i] - '0') + (B[i] - '0') + tmp) % 10) + ans;
45             tmp = 1;
46         }else
47         {
48             ans = static_cast<char>('0' + ((A[i] - '0') + (B[i] - '0') + tmp)) + ans;
49             tmp = 0;
50         }
51     }
52     if (tmp) ans = '1' + ans;
53     cout << ans << endl;;
54 }
55 
56 int main()
57 {
58 
59     while (cin >> A >> B)
60     {
61         ans.clear();
62         Repair();
63         Solve();
64     }
65 
66 #ifdef HOME
67     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
68 #endif
69     return 0;
70 }

3117 高精度练习之乘法:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cmath>
  5 #include <string>
  6 #include <algorithm>
  7 #include <cstring>
  8 #include <set>
  9 #include <utility>
 10 #include <locale>
 11 #include <ctime>
 12 using namespace std;
 13 //using int64 = long long;
 14 const int INF = 0x3f3f3f3f;
 15 const int MaxN = 110;
 16 
 17 string _A, _B;
 18 
 19 void Repair(string& A, string& B)
 20 {
 21     if (A.size() < B.size())
 22     {
 23         for (int i = A.size(); i < B.size(); ++i)
 24         {
 25             A = '0' + A;
 26         }
 27     }else
 28     {
 29         for (int i = B.size(); i < A.size(); ++i)
 30         {
 31             B = '0' + B;
 32         }
 33     }
 34 }
 35 
 36 string Add(string A, string B)
 37 {
 38     Repair(A, B);
 39     string ans;
 40     int tmp = 0;
 41     for (int i = A.size() - 1; i >= 0; --i)
 42     {
 43         if ((A[i] - '0') + (B[i] - '0') + tmp > 9)
 44         {
 45             ans = static_cast<char>('0' + ((A[i] - '0') + (B[i] - '0') + tmp) % 10) + ans;
 46             tmp = 1;
 47         }
 48         else
 49         {
 50             ans = static_cast<char>('0' + ((A[i] - '0') + (B[i] - '0') + tmp)) + ans;
 51             tmp = 0;
 52         }
 53     }
 54     if (tmp) ans = '1' + ans;
 55     return ans;
 56 }
 57 
 58 string Mul(string A, char B)
 59 {
 60     string ans;
 61     int tmp = 0;
 62     int result;
 63     for (int i = A.size() - 1; i >= 0; --i)
 64     {
 65         result = (A[i] - '0')*(B - '0') + tmp;
 66         tmp = result / 10;
 67         ans = static_cast<char>('0' + result % 10) + ans;
 68     }
 69     if (tmp) ans = static_cast<char>('0' + tmp) + ans;
 70     return ans;
 71 }
 72 
 73 void Solve(string A, string B)
 74 {
 75     string ans = "0";
 76     string tmp;
 77     int num = 0; // 统计加的0的个数
 78     for (int i = B.size() - 1; i >= 0; --i)
 79     {
 80         tmp = Mul(A, B[i]);
 81         for (int j = 0; j < num; ++j) tmp += '0';
 82         ans = Add(ans, tmp);
 83         ++num;
 84     }
 85     cout << ans << endl;;
 86 }
 87 
 88 int main()
 89 {
 90 
 91     while (cin >> _A >> _B)
 92     {
 93         Solve(_A, _B);
 94     }
 95 
 96 #ifdef HOME
 97     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
 98 #endif
 99     return 0;
100 }

 

posted @ 2015-09-30 02:07  JmingS  阅读(294)  评论(0编辑  收藏  举报