『高精度正整数加法』题解 | #高精度整数加法#
高精度整数加法
http://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include<bits/stdc++.h> using namespace std; int main() { string one,two; while( cin>>one>>two ) { string res; reverse( one.begin(), one.end() ); reverse( two.begin(), two.end() ); //进位 int carry=0; int posOne=0,posTwo=0; while( posOne<one.size() && posTwo<two.size() ) { int temp=(one[ posOne ]-'0') +( two[ posTwo ]-'0' )+carry ; carry=temp/10; temp%=10; res+=(temp+'0'); ++posOne; ++posTwo; } while( posOne<one.size() ) { int temp=(one[ posOne ]-'0') +carry ; carry=temp/10; temp%=10; res+=(temp+'0'); ++posOne; } while( posTwo<two.size() ) { int temp=( two[ posTwo ]-'0' )+carry ; carry=temp/10; temp%=10; res+=(temp+'0'); ++posTwo; } if( carry ) { res+='1'; } reverse( res.begin(), res.end() ); cout<<res<<endl; one.clear(); two.clear(); } return 0; }