Compare commits
2 Commits
07b74d0957
...
1e85870eb8
| Author | SHA1 | Date | |
|---|---|---|---|
| 1e85870eb8 | |||
| 436c46d851 |
140
高精算法---高精加法修正/zeng.md
Normal file
140
高精算法---高精加法修正/zeng.md
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
### 原本代码
|
||||||
|
|
||||||
|
``` cpp
|
||||||
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
string re(string x){
|
||||||
|
reverse(x.begin(),x.end());
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sum_(int a,int b){
|
||||||
|
return (a+b)%10;
|
||||||
|
}
|
||||||
|
int plus_(int a,int b){
|
||||||
|
return ((a+b)/10>0) ? 1:0;
|
||||||
|
}
|
||||||
|
|
||||||
|
string main_(string a,string b){
|
||||||
|
string ans ="";
|
||||||
|
string n=a;
|
||||||
|
a=(a.size()<b.size()) ? b:a; b=(a.size()<b.size()) ? n:b;
|
||||||
|
a=re(a);b=re(b);
|
||||||
|
int m = a.size();
|
||||||
|
int plus = 0;
|
||||||
|
for(int i=0;i<m;i++){
|
||||||
|
int sum = 0;
|
||||||
|
if(i>b.size()){
|
||||||
|
sum = sum_(a[i]-'0',0);
|
||||||
|
ans+=char(sum+'0'+plus);
|
||||||
|
plus=plus_(a[i]-'0',0);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
sum = sum_(a[i]-'0',b[i]-'0');
|
||||||
|
ans+=char(sum+'0'+plus);
|
||||||
|
plus=plus_(a[i]-'0',b[i]-'0');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ans=re(ans);
|
||||||
|
return ans;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
signed main(){
|
||||||
|
string a,b;
|
||||||
|
cin>>a>>b;
|
||||||
|
cout<<main_(a,b);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 修正代码
|
||||||
|
|
||||||
|
``` cpp
|
||||||
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// 字符串反转函数
|
||||||
|
string re(string x){
|
||||||
|
reverse(x.begin(), x.end());
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 求个位
|
||||||
|
int sum_(int a,int b){
|
||||||
|
return (a + b) % 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断是否进位
|
||||||
|
int plus_(int a,int b){
|
||||||
|
return ((a + b) / 10 > 0) ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 高精度加法主函数
|
||||||
|
string main_(string a,string b){
|
||||||
|
string ans = "";
|
||||||
|
string n = a;
|
||||||
|
|
||||||
|
// 保证a为较长的字符串
|
||||||
|
a = (a.size() < b.size()) ? b : a;
|
||||||
|
b = (a.size() < b.size()) ? n : b;
|
||||||
|
|
||||||
|
a = re(a); b = re(b); // 反转便于从低位加起
|
||||||
|
|
||||||
|
int m = a.size();
|
||||||
|
int plus = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < m; i++){
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
// 错误判断:i > b.size() 实际应该是 i >= b.size()
|
||||||
|
// 原错误代码:
|
||||||
|
// if(i > b.size()){
|
||||||
|
// 修正代码:
|
||||||
|
if(i >= b.size()){ // 修正:防止访问越界
|
||||||
|
// 原错误:sum_(a[i]-'0', 0) + 直接加 plus 到字符上
|
||||||
|
// sum = sum_(a[i] - '0', 0);
|
||||||
|
// ans += char(sum + '0' + plus); // 错误:把 plus 加在字符上
|
||||||
|
// plus = plus_(a[i] - '0', 0);
|
||||||
|
|
||||||
|
// 修正:将 plus 加入总和,再统一处理进位
|
||||||
|
sum = (a[i] - '0') + plus;
|
||||||
|
ans += char((sum % 10) + '0');
|
||||||
|
plus = sum / 10;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
// 原错误同上:进位加在字符编码上,逻辑错
|
||||||
|
// sum = sum_(a[i] - '0', b[i] - '0');
|
||||||
|
// ans += char(sum + '0' + plus);
|
||||||
|
// plus = plus_(a[i] - '0', b[i] - '0');
|
||||||
|
|
||||||
|
// 修正如下
|
||||||
|
sum = (a[i] - '0') + (b[i] - '0') + plus;
|
||||||
|
ans += char((sum % 10) + '0');
|
||||||
|
plus = sum / 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理最高位进位
|
||||||
|
if(plus) ans += char(plus + '0');
|
||||||
|
|
||||||
|
ans = re(ans); // 反转回来得到最终结果
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
signed main(){
|
||||||
|
string a, b;
|
||||||
|
cin >> a >> b;
|
||||||
|
cout << main_(a, b);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user