sdnuoj 1041. Arbitrary base conversion

Method 1: Divide n by modulo n

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <sstream>
#include <map>
#include <set>
typedef long long ll;
using namespace std;
int main()
{
    
    
    int n, m, i;
    int ar[10005];
    cin>>n>>m;
    for(i = 0; n; i++)
    {
    
    
        ar[i] = n%m;
        n = n/m;
    }
    for(int j = i-1; j>=0; j--)
    {
    
    
        cout<<ar[j];
    }

    return 0;
}


Method 2: Use the stack to realize the
stack can be used as a potato chip bottle, standing upright, assuming that the merchant is boring, put the potato chips in the bottle piece by piece, and the game at the bottom of the stack first, and the game at the top, and then we eat When it’s time, it’s taken from the top, and it’s impossible to take the bottom through the potato chips without breaking the bottle.
Definition and usage

stack<int>s;
s.empty();//判断栈是否为空,空则返回true,否则返回Flase while(!s.empty())
s.size();//返回栈的元素的个数
s.push();//将元素从栈顶塞进去
s.pop();//将栈顶的元素搞出去
s.top();//返回栈顶的元素,这个时候栈顶的元素是没有pop出去的

```cpp
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <sstream>
#include <map>
#include <set>
typedef long long ll;
using namespace std;
int main()
{
    
    
    stack<int>s;
    int n, m;
    cin>>n>>m;
    while(n)
    {
    
    
        s.push(n%m);
        n = n/m;
    }
    while(!s.empty())
    {
    
    
        cout<<s.top();
        s.pop();
    }
    return 0;
}












Guess you like

Origin blog.csdn.net/weixin_51216553/article/details/109538746