求每一位数和

#include <iostream>
#include <stack>
using namespace std;
int main()
{
    int r,n,ans=0;
    stack<int>s;
    cin>>n>>r;
    while(n)
    {
        s.push(n%r);
        n/=r;
    }
    while(!s.empty())
    {
        ans+=s.top();
        s.pop();
    }
    cout<<ans<<endl;
    return 0;
}

 

posted @ 2019-08-05 13:04  zhanghua-01  阅读(116)  评论(0编辑  收藏  举报