zoukankan      html  css  js  c++  java
  • HDU 2031 进制转换

    进制转换

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 58687    Accepted Submission(s): 31957


    Problem Description
    输入一个十进制数N,将它转换成R进制数输出。
     
    Input
    输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
     
    Output
    为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
     
    Sample Input
    7 2 23 12 -4 3
     
    Sample Output
    111 1B -11
     
    Author
    lcy
     
    Source
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<set>
    #include<map>
    #include<sstream>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<list>
    #include<vector>
    #include<string>
    using namespace std;
    #define long long ll
    const double PI = acos(-1.0);
    const double eps = 1e-6;
    const int inf = 0x3f3f3f3f;
    const int N = 500005;
    int n, m, tot;
    int a[N];
    stack<char> s;
    int main()
    {
        while(cin >> n >> m )
        {
            if(n<0)
                cout<<"-";
            int tmp;
            while(n)
            {
                tmp = n%m;
                tmp = tmp>0?tmp:-tmp;
                if(tmp<10) s.push(tmp +'0');
                else s.push(tmp - 10 + 'A');
                n /= m;
            }
            while(!s.empty())
            {
                cout<<s.top();
                s.pop();
            }
            cout<<endl;
        }
    }
    除基取余+satck逆序输出
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<set>
    #include<map>
    #include<sstream>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<list>
    #include<vector>
    #include<string>
    using namespace std;
    #define long long ll
    const double PI = acos(-1.0);
    const double eps = 1e-6;
    const int inf = 0x3f3f3f3f;
    const int N = 500005;
    int n, m, tot;
    int a[N];
    stack<char> s;
    void print(int a)
    {
        if(a<10) cout<<a;
        else cout<<(char)(a-10+'A');
    }
    int main()
    {
        while(cin >> n >> m )
        {
            if(n<0)
                cout<<"-",n=-n;
            int tmp, k = 0;
            while(n)
            {
                a[++k] = n % m;
                n /= m;
            }
            for(int i=k; i>=1; i--)
            {
                print(a[i]);
            }
            cout<<endl;
        }
    }
    View Code
  • 相关阅读:
    freemarker ,DEFAULT_INCOMPATIBLE_IMPROVEMENTS 找不到
    git clone的时候filename too long解决办法
    什么是CPU密集型、IO密集型?
    Mysql中联合索引的最左匹配原则(百度)
    linux利用grep查看打印匹配的下几行或前后几行的命令
    手机号码验证的正则表达式
    Http、Socket、WebSocket之间联系与区别
    thrift 是rpc协议
    面试小结之Elasticsearch篇
    maven 左边空了
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8716694.html
Copyright © 2011-2022 走看看