题目链接:
https://pintia.cn/problem-sets/994805260223102976/problems/994805299301433344
思路:进制转换类题目,用一个栈存储每次除以D后取的余数,再进行输出。
有关进制转换的图解,请看https://www.cnblogs.com/gaizai/p/4233780.html
填坑日记:。。。。本题没看到坑
代码:
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int A,B,D,m,i;
stack<int> s;
cin>>A>>B>>D;
m=A+B;
do
{
s.push(m%D);
m=m/D;
}while(m!=0);
while(!s.empty())
{
cout<<s.top();
s.pop();
}
return 0;
}
好了现在来让我们观摩一下大佬的代码,链接:https://www.liuchuo.net/archives/547
代码:
#include <iostream>
using namespace std;
int main() {
int a, b, d;
cin >> a >> b >> d;
int t = a + b;
if (t == 0) {
cout << 0;
return 0;
}
int s[100];
int i = 0;
while (t != 0) {
s[i++] = t % d;
t = t / d;
}
for (int j = i - 1; j >= 0; j--) {
cout << s[j];
}
return 0;
}