高精度减法c++代码
时间: 2023-11-04 11:05:29 浏览: 83
好的,以下是高精度减法的C++代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
const int N = 10010;
int a[N], b[N], c[N];
int main()
{
string s1, s2;
cin >> s1 >> s2;
int len1 = s1.size(), len2 = s2.size();
for (int i = 0; i < len1; i ++ ) a[i] = s1[len1 - i - 1] - '0';
for (int i = 0; i < len2; i ++ ) b[i] = s2[len2 - i - 1] - '0';
int len = max(len1, len2);
for (int i = 0; i < len; i ++ )
{
c[i] += a[i] - b[i];
if (c[i] < 0)
{
c[i] += 10;
c[i + 1] --;
}
}
while (len > 1 && !c[len - 1]) len -- ;
for (int i = len - 1; i >= 0; i -- ) cout << c[i];
cout << endl;
return 0;
}
```
阅读全文