题解 | #又一版 A+B#
又一版 A+B
https://www.nowcoder.com/practice/9255c05d45b8406c9b588d7c57aa920b
#include<bits/stdc++.h> using namespace std; string add(int A, int B) { int rest= 0; string res= ""; while(A!= 0&& B!= 0) { int a= A%10; int b= B%10; rest= a+ b+ rest; A/= 10; B/= 10; res= (char)(rest%10+ '0')+ res; rest/= 10; } if(A== 0){ B+= rest; } else { A+= rest; } while(A!= 0) { res= (char)(A%10+ '0')+ res; A/= 10; } while(B!= 0) { res= (char)(B%10+ '0')+ res; B/= 10; } return res; } // 返回余数 int divide(string &str, int m) { string res= ""; int rest= 0; for(int i=0; i<str.length(); i++) { rest= rest*10+ str[i]-'0'; res+= (char)(rest/m+ '0'); rest%= m; } // cout<< str<< "/"<< m<< "="<< res<< "..."<< rest<< endl; int idx= 0; while(res[idx]== '0') { idx++; } str= res.substr(idx); if(str.length()<1) { str= "0"; } return rest; } string trans(string str, int m) { string res= ""; while(str.compare("0")!= 0) { res= (char)(divide(str, m)+'0')+ res; } // cout<< "str="<< str<< endl; return res; } int main() { int m, A, B; while(cin>> m) { if(m== 0) break; cin>> A>> B; string plus= add(A, B); cout<< trans(plus, m)<< endl; } return 0; }