20pts C++求条
查看原帖
20pts C++求条
1194403
linyunxiang1111楼主2025/1/21 20:20
#include<bits/stdc++.h>
using namespace std;
string a,b;
char c[5001];
int main() {
	cin >> a;
	cin >> b;
    bool is_a_dayu_b = false;
	int len1 = a.size(),len2 = b.size();
	if(a < b) {
		string t=a;a=b;b=t;
        is_a_dayu_b = true;
	}
    if(len2 < len1) {
		for(int i=1; i<=len1-len2; i++)
			b="0"+b;
	}
	int len3 = max(len1,len2);
	bool ac = false;
	for(int i=len3-1; i>=0; i--) {
		int temp = a[i] - b[i];
		if(ac == true) {
			temp--;
			ac = false;
		}
		if(temp<0) {
			ac=true;
			temp+=10;
		}
		c[i] = temp + '0';
	}
    for(int i=0;c[i]=='0';i++)
        c[i] = '&';//&没用,就是个标记
    if(is_a_dayu_b == true)
        cout << "-";
	for(int j=0;j<len3;j++)
		if(c[j]!='&') 
            cout << c[j];
    if(c[len3-1]=='&')
        cout << "0" << endl;
	return 0;
}
2025/1/21 20:20
加载中...