学习目标
高精度加法
[高精度加法]
#include<bits/stdc++.h> using namespace std; int main(){ string a; string b; int c[10089]={0}; int d[10089]={0}; int e[10089]={0}; cin>>a>>b; int l1 = a.size() , l2 = b.size(); for(int i=0;i<l1;i++){ c[i]=a[l1-i-1]-'0'; } for(int i=0;i<l2;i++){ d[i]=b[l2-i-1]-'0'; } int l=max(l1,l2),jw=0; for(int i=0;i<l;i++){ e[i]=c[i]+d[i]+jw; if(e[i]>9){ e[i]-=10; jw=1; }else{ jw=0; } } if(jw) cout<<1; for(int i=0;i<l;i++){ cout<<e[l-i-1]; } return 0; }View Code
高精度减法
#include<bits/stdc++.h> using namespace std; int main(){ string s1,s2; cin >> s1 >> s2; if(s2.size() > s1.size() || s1.size() == s2.size() && s1 < s2) { swap(s1,s2); cout << "-"; } int l1 = s1.size(),l2 = s2.size(); int a1[10001] = {0},a2[10001] = {0},c[10001] = {0}; for(int i = 0; i < l1 ; i++){ a1[i] = s1[l1-i-1] - '0'; } for(int i = 0; i < l2 ; i++){ a2[i] = s2[l2-i-1] - '0'; } for(int i = 0;i < l1 ;i++){ c[i] = a1[i] - a2[i]; if(c[i] < 0){ c[i] += 10; a1[i + 1] -= 1; } } while(c[l1] == 0 && l1 > 0) { //去零 l1-- ; } while(l1 >= 0){ //倒序输出 cout << c[l1--]; } return 0; }View Code
标签:cout,高精度,int,s2,U7,加减,C++,s1,size From: https://www.cnblogs.com/jayxuan/p/18148912