题目链接:1022 D进制的A+B (20 分)
这道题目比较简单。思路:先计算出十进制的A+B,然后利用栈的后进先出的特点输出D进制结果。
在进制转化过程中采用除基取余的方法。特别注意:特殊情况A+B=0的时候要单独处理!
1 #include <bits/stdc++.h>
2 using namespace std;
3
4 int main()
5 {
6 int a,b,d;
7 stack<
int>
ans;
8 cin>>a>>b>>
d;
9 a=a+
b;
10 if(a==
0)
//一种特殊情况
11 cout<<
0;
12 //<<多了一个换行竟然也是格式错误。
13 while(a)
14 {
15 ans.push(a%
d);
16 a/=
d;
17 }
18 while(!
ans.empty())
19 {
20 cout<<
ans.top();
21 ans.pop();
22 }
23 cout<<
endl;
24 return 0;
25 }
转载于:https://www.cnblogs.com/ManOK/p/10188613.html