|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 柿子饼同学 于 2022-5-12 22:07 编辑
- #include <bits/stdc++.h>
- using namespace std;
- string sub(string a, string b){
- bool ism = 0;
- int l, dl, c = 0;
- if(a.size() < b.size()){
- swap(a, b);
- ism = 1;
- }
- if(a.compare(b) < 0 && a.size() == b.size()){
- swap(a, b);
- ism = 1;
- }
- l = b.size();
- dl = a.size() - b.size();
- for(int i = l-1; i >= 0; i--){
- if(a[i+dl]-'0' - c < b[i]-'0'){
- ans = char(a[i+dl] + 10 - b[i] - c + '0') + ans;
- c = 1;
- }
- else{
- ans = char(a[i+dl] - b[i] - c + '0') + ans;
- c = 0;
- }
- }
- ans.erase(0, ans.find_first_not_of('0'));
- if(!ism) ans = '-' + ans;
- return ans;
- }
- int main(){
- ios::sync_with_stdio(0);
- string a, b;
- cin >> a >> b;
- cout << sub(a, b) << endl;
- return 0;
- }
复制代码
我的高精减法代码如上, 不知道哪里出了问题, 求指点
本帖最后由 jhq999 于 2022-5-13 08:51 编辑
- #include <iostream>
- #include <string>
- using namespace std;
- string sub(string a, string b){
- bool ism = 0;
- int l, dl;
- if(a.size() < b.size()||(a.compare(b) < 0 && a.size() == b.size())){
- swap(a, b);
- ism = 1;
- }
- l = a.size();
- dl = a.size() - b.size();
- for(int i = l-1; i >= 0; i--){
- //if(a[i]<'0')a[i]+=10,a[i-1]--;
- if(i>=dl)
- {
- if(a[i]<b[i-dl])
- {
- a[i]+=10;
- a[i-1]--;
- }
- a[i]=a[i]-b[i-dl]+'0';
- }
- else if(a[i]<'0')
- a[i]+=10,a[i-1]--;
- }
- b="";
- if(ism)b="-";
- b+=a;
- return b;
- }
- int main(){
- ios::sync_with_stdio(0);
- std::string a, b;
- std::cin >> a >> b;
- cout << sub(a, b) << endl;
- return 0;
- }
复制代码
|
|