SDNUOJ——1211.使用棧實現進制轉換

問題蟲洞:1211.使用棧實現進制轉換

 

黑洞內窺:

使用棧將一個很長(>30)的十進制數轉換爲二進制數

 

思路光年:

模擬

 

ACcode:

#include<stdio.h>
#include<iostream>
#include<map>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<math.h>
#include<stack>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
#define MAXN 100005*2
#define INF 0x3f3f3f3f//將近int類型最大數的一半,而且乘2不會爆int
#define MOD 1000000007 // MOD%4 = 3
const double pi = acos(-1.0);
const double eps = 1e-6;

int main()
{
    string s;
    while(cin >> s)
    {
        stack<char>st;
        while(s > "0")
        {
            int ans = 0, yv = 0, len = s.size(), is=0;
            for(int i=0; i<len; ++i)        //大數mod2,且/2
            {
                ans = yv*10 + s[i] - '0';
                s[i] = ans/2 + '0';
                yv = ans%2;
            }
            st.push(yv+'0');                //餘數進棧
            while(s[is] == '0')            //去掉前導零
                is++;
            s.erase(0, is);
        }
        while(!st.empty())
        {
            char cnt = st.top();
            st.pop();
            cout << cnt;
        }
        cout << '\n';
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章