输入两个非负 10 进制整数 A 和 B (≤2?30??−1), 输出 A+B 的 D (1<D≤10) 进制数.
输入格式:
输入在一行中依次给出 3 个整数 A,B 和 D.
输出格式:
输出 A+B 的 D 进制数.
输入样例:
123 456 8
输出样例:
- 1103
- #include<iostream>
- #include<string>
- using namespace std;
- int main()
- {
- int a,b;
- cin>>a>>b;
- int d;
- cin>>d;
- string str;
- int x=a+b;
- if(x==0) str='0';
- else
- while(x)
- {
- int y=x%d;
- char c=y+48;
- str+=c;
- //cout<<x<<endl;
- x/=d;
- }
- for(int i=str.length()-1;i>=0;i--) cout<<str[i];
- return 0;
- }
这道题应该注意的点是 A+B 等于的 0 情况
来源: http://www.bubuko.com/infodetail-3289792.html