B1022. D进制的A+B

除基取余法

#include<bits/stdc++.h>
using namespace std;
stack<int> s;
int main(){
    long long a,b;
    int d;
    cin>>a>>b>>d;
    long c=a+b;
    while(c>0){
        s.push(c%d);
        c=c/d;
    }
    while(!s.empty()){
        cout<<s.top();
        s.pop();
    }
    return 0;
}

上述得分18/20,这是因为没考虑全。比如a+b=0的情况,它应该输出0。而我们的程序不会输出。

#include<bits/stdc++.h>
using namespace std;
stack<int> s;
int main(){
    long long a,b;
    int d;
    cin>>a>>b>>d;
    long c=a+b;
    if(c==0){cout<<0;return 0;}
    while(c>0){
        s.push(c%d);
        c=c/d;
    }
    while(!s.empty()){
        cout<<s.top();
        s.pop();
    }
    return 0;
}
posted @ 2019-03-18 11:35  MarkKobs  阅读(266)  评论(0编辑  收藏  举报