sdnuoj 1041.任意进制转换

方法一:模n除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;
}


法二:用栈实现
栈 可以当做一个薯片瓶,竖着的,假设商家比较无聊,一片片的往瓶里赛薯片,就先赛的在栈底,后赛的在上面,然后我们吃的时候,就是从上面取,不可能不破坏瓶子的情况下,隔着上面的薯片去取最下面的
定义与用法

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












猜你喜欢

转载自blog.csdn.net/weixin_51216553/article/details/109538746