求每一位数和

Posted zhanghua-322

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求每一位数和相关的知识,希望对你有一定的参考价值。

#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;

 

以上是关于求每一位数和的主要内容,如果未能解决你的问题,请参考以下文章