鱼C论坛

 找回密码
 立即注册
查看: 1951|回复: 1

[已解决]高精度减法

[复制链接]
发表于 2022-5-12 22:06:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 柿子饼同学 于 2022-5-12 22:07 编辑
  1. #include <bits/stdc++.h>
  2. using namespace std;

  3. string sub(string a, string b){
  4.         bool ism = 0;
  5.         int l, dl, c = 0;
  6.         if(a.size() < b.size()){
  7.                 swap(a, b);
  8.                 ism = 1;
  9.         }
  10.         if(a.compare(b) < 0 && a.size() == b.size()){
  11.                 swap(a, b);
  12.                 ism = 1;
  13.         }
  14.         l = b.size();
  15.         dl = a.size() - b.size();
  16.         for(int i = l-1; i >= 0; i--){
  17.                 if(a[i+dl]-'0' - c < b[i]-'0'){
  18.                         ans = char(a[i+dl] + 10 - b[i] - c + '0') + ans;
  19.                         c = 1;
  20.                 }
  21.                 else{
  22.                         ans = char(a[i+dl] - b[i] - c + '0') + ans;
  23.                         c = 0;
  24.                 }
  25.         }
  26.         ans.erase(0, ans.find_first_not_of('0'));
  27.         if(!ism) ans = '-' + ans;
  28.         return ans;
  29. }

  30. int main(){
  31.         ios::sync_with_stdio(0);
  32.         string a, b;
  33.         cin >> a >> b;
  34.         cout << sub(a, b) << endl;
  35.         return 0;
  36. }
复制代码

我的高精减法代码如上, 不知道哪里出了问题, 求指点
最佳答案
2022-5-13 08:44:32
本帖最后由 jhq999 于 2022-5-13 08:51 编辑
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. string sub(string a, string b){
  5.         bool ism = 0;
  6.         int l, dl;
  7.         if(a.size() < b.size()||(a.compare(b) < 0 && a.size() == b.size())){
  8.                swap(a, b);
  9.                ism = 1;
  10.         }
  11.         l = a.size();
  12.         dl = a.size() - b.size();
  13.         for(int i = l-1; i >= 0; i--){
  14.                 //if(a[i]<'0')a[i]+=10,a[i-1]--;
  15.                 if(i>=dl)
  16.                 {
  17.                         if(a[i]<b[i-dl])
  18.                         {
  19.                                 a[i]+=10;
  20.                                 a[i-1]--;
  21.                         }
  22.                         a[i]=a[i]-b[i-dl]+'0';
  23.                  }
  24.                 else if(a[i]<'0')
  25.                         a[i]+=10,a[i-1]--;               
  26.         }
  27.         b="";
  28.         if(ism)b="-";
  29.         b+=a;
  30.         return b;
  31. }

  32. int main(){
  33.         ios::sync_with_stdio(0);
  34.         std::string a, b;
  35.         std::cin >> a >> b;
  36.         cout << sub(a, b) << endl;
  37.         return 0;
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-13 08:44:32 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-5-13 08:51 编辑
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. string sub(string a, string b){
  5.         bool ism = 0;
  6.         int l, dl;
  7.         if(a.size() < b.size()||(a.compare(b) < 0 && a.size() == b.size())){
  8.                swap(a, b);
  9.                ism = 1;
  10.         }
  11.         l = a.size();
  12.         dl = a.size() - b.size();
  13.         for(int i = l-1; i >= 0; i--){
  14.                 //if(a[i]<'0')a[i]+=10,a[i-1]--;
  15.                 if(i>=dl)
  16.                 {
  17.                         if(a[i]<b[i-dl])
  18.                         {
  19.                                 a[i]+=10;
  20.                                 a[i-1]--;
  21.                         }
  22.                         a[i]=a[i]-b[i-dl]+'0';
  23.                  }
  24.                 else if(a[i]<'0')
  25.                         a[i]+=10,a[i-1]--;               
  26.         }
  27.         b="";
  28.         if(ism)b="-";
  29.         b+=a;
  30.         return b;
  31. }

  32. int main(){
  33.         ios::sync_with_stdio(0);
  34.         std::string a, b;
  35.         std::cin >> a >> b;
  36.         cout << sub(a, b) << endl;
  37.         return 0;
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 09:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表