• hdu 2031 进制转换(栈思想的使用)


    进制转换

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


    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
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2035 2043 2036 2048 2049 
     
     
    ac代码:  (栈思想,先进后出)
     
     
     
     

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<stack>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define Swap(a,b,t) t=a,a=b,b=t
    #define Mem0(x) memset(x,0,sizeof(x))
    #define Mem1(x) memset(x,-1,sizeof(x))
    #define MemX(x) memset(x,0x3f,sizeof(x));
    using namespace std;
    typedef long long ll;
    const int inf=0x3f3f3f;
    const double eps=1e-12;
    stack <int> q;
    int main()
    {
    int n,d;
    while (cin>>n>>d){
    while (!q.empty())
    q.pop();
    if (n<0){
    cout<<"-";
    n=-n;
    }
    else if (!n){
    cout<<'0'<<endl;
    continue;
    }
    while (n){
    q.push(n%d);
    n/=d;
    }
    while (!q.empty()){
    int x=q.top();
    if (x>=10)
    printf("%c",'A'+x-10);     //cout<<'A'+x-10<<endl;         这个地方有坑,    这里   cout    输出的不是字符,而是整数.
    else
    cout<<x;
    q.pop();
    }
    cout<<endl;
    }
    return 0;
    }

  • 相关阅读:
    Javascript Read Excel
    Rest API 操作List Items
    web安全入门课程笔记——SQL漏洞分析与利用
    web安全入门课程笔记——网站基础与信息搜集
    博客迁移通知
    Python查找指定文件
    博客园写作避坑指南【持续更新】
    Changes of user relationship in AD can't be correctly synchronized to SCSM
    博客地址改为 https://0xcreed.jxustctf.top
    AI:WEB:1 Walkthrough
  • 原文地址:https://www.cnblogs.com/q1204675546/p/9721514.html
Copyright © 2020-2023  润新知