题号:2142
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include<iostream> #include<string> using namespace std; void reverse(string & s){ for(int i=0;i<s.size()/2;i++){ swap(s[i],s[s.size()-1-i]); } } int main(){ string s1,s2; cin>>s1>>s2; bool negative=false; if(s1.length()>s2.length()) swap(s1,s2); else if(s1.length()==s2.length()){ if(s1>=s2)swap(s1,s2); else negative=true; }else negative=true; reverse(s1); reverse(s2); int s1len=s1.length(),s2len=s2.length(); for(int i=0;i<s1len;i++){ s2[i]-=s1[i]-'0'; } for(int i=0;i<s2len-1;i++){ if(s2[i]<'0'){ s2[i+1]-=1; s2[i]+=10; } } if(s2[s2len-1]=='0'){ s2.erase(s2.end()-1); } while(s2[s2.size()-1]=='0'&&s2.size()>1) s2.erase(s2.end()-1); reverse(s2); if(negative)cout<<"-"; cout<<s2; } |