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
  • 相关阅读:
    C# 操作Orcle数据库
    WinDbg排查CPU高的问题
    NetCore微服务实战体系:日志管理
    NetCore微服务实战体系:Grpc+Consul 服务发现
    解惑求助-关于NetCore2.2中间件响应的问题
    EF Join连接查询的坑
    给DataTable添加行的几种方式
    [C#] 折腾海康威视的人体测温 模组
    [WPF 学习] 15.播放百度合成的语音
    [WPF 学习] 14.PlaceHolder的简单实现
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8716694.html
Copyright © 2011-2022 走看看