1022 D进制的A+B (20分)

注意对相加为\(0\)的特殊处理,结果一定为\(0\)

如果进制转换时使用的是while语句而不是do...while语句,那么要注意当A+B为0时需要特判输出0。

int a,b,d;

int main()
{
    cin>>a>>b>>d;

    int sum=a+b;

    vector<int> res;
    if(sum == 0) res.pb(0);
    while(sum)
    {
        res.pb(sum%d);
        sum/=d;
    }
    reverse(res.begin(),res.end());
    for(auto t:res) cout<<t;
    cout<<endl;
    //system("pause");
    return 0;
}
posted @ 2021-01-30 17:27  Dazzling!  阅读(31)  评论(0编辑  收藏  举报