为什么用cin读入就能AC,用getline读入只有十分
查看原帖
为什么用cin读入就能AC,用getline读入只有十分
778339
rush0821楼主2024/1/18 21:12
#include<bits/stdc++.h> 

using namespace std;
bool f=0;
string s1,s2;
bool cmp(string a, string b) {
	if (a.size() != b.size())
		return a.size() < b.size() ;
	else
		return a < b ;
}
string jian(string a, string b) {
	f=0;
	string c = a.size() > b.size() ? a : b;//取长度较长者
	if(cmp(a,b)){
		f=1;
//		cout<<"------_"<<endl;
		cout<<"-";
		swap(a,b);
	}
	int i = a.size() - 1, j = b.size() - 1, k = c.size() - 1;
	while(i>=0){
//		cout<<"i "<<i<<"  j "<<j<<endl;
//		cout<<a[i]<<" "<<b[j]<<endl;
		if(j>=0){
			if(a[i]<b[j]){
				a[i]+=10;
				a[i-1]--;
			}
			c[k--] = a[i--] - b[j--] + '0';
		}else{
//			c[k--]=a[i--];
			if(a[i]<'0'){
				a[i]+=10;
				a[i-1]--;
			}
			c[k--]=a[i--];
		}
	}
	int m=0,t=c.size();
	while(c[m]=='0' && m<t-1)m++;
	return c.substr(m, t - m);
}

int main( )
{
//	freopen("P2142_1.in","r",stdin);
    getline(cin,s1);
    getline(cin,s2);
//	cin>>s1>>s2;
    cout<<jian(s1,s2);
    return 0;
}
2024/1/18 21:12
加载中...