#include <iostream> #include <stdio.h> #include <math.h> #include <string> using namespace std; void pp(string s1,string s2){ int maxl = s1.length()>s2.length()?s1.length():s2.length(); int minl = s1.length()<s2.length()?s1.length():s2.length(); string maxs,mins; if(maxl == s1.length()){ maxs = s1; mins = s2; }else{ maxs = s2; mins = s1; } for(int i = 0;i < maxl - minl;i++){ mins = "0"+mins; } mins = "0"+mins; maxs = "0"+maxs; char a[10000]; for(int i = 0;i < 10000;i++){ a[i] = '0'; } int count = 0; for(int i = maxs.length() - 1;i>=0;i--){ a[i] = mins[i] + maxs[i] - '0' + count; count = 0; if(a[i] >= '9'+1){ a[i] -= 10; count = 1; } } for(int i = 0;i < maxs.length();i++){ if(a[0] == '0'&&i == 0){ continue; }else{ cout << a[i]-'0'; } } } int main(){ string s1,s2; cin >> s1; cin >> s2; pp(s1,s2); }
算法初步P4.高精度加法
最新推荐文章于 2024-11-05 21:58:11 发布