1022 D进制的A+B

使用了STL中的stack,常用操作的时间复杂度大都是O(1)。

总体比较水。考察进制转换的除基取余法。

#include"iostream"
#include<stack>
using namespace std;
int main() {
    int a,b,d,sum;
    cin>>a>>b>>d;
    stack<int> st;
    sum = a + b;
    if(sum == 0) //sum为0要特判 
        cout<<sum;
    while( sum !=  0) {//除基取余法
        st.push(sum%d);//把余数压入栈底。
        sum/=d;
    }
    while(st.empty() == false) {
        cout<<st.top();//访问栈顶元素
        st.pop();//弹出栈顶元素
    }
    return 0;
}

 

posted @ 2020-02-16 16:31  tangq123  阅读(90)  评论(0编辑  收藏  举报