acm 2031

进制转换

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


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
 
 
 
#include "stdio.h"
#include <map>
#include <iostream>
#include <string>


using namespace std;

int main()
{
    int num , decimal,chushu,yushu,i;
    map<int,char> bitmap;
    char ch;
    bool single = false;
    bool IsZheng;
    while(scanf("%d %d",&num,&decimal) != EOF)
    {
        bitmap.clear();
        if(num < 0)
        {
            num = 0 - num;
            IsZheng = false;
        }else
        {
            IsZheng = true;
        }
        i = 0;
        chushu = num / decimal;
        single = false;
        while(1)
        {
            yushu = num % decimal;
            if(yushu > 9)
            {
                ch = yushu - 10 + 65;
                bitmap.insert(pair<int,char>(i,ch));

            }else
            {
                ch = yushu + 48;
                bitmap.insert(pair<int,char>(i,ch));
            }
            num = chushu;
            if(single == true)
            {
                break;
            }
            chushu = num / decimal;
            if(chushu == 0)
            {
                single = true;
            }
            i++;
        }
        i = 0;
        if(!IsZheng)
        {
            cout << "-";
        }
        for (map<int, char>::reverse_iterator num = bitmap.rbegin(); num != bitmap.rend(); num++)
         {
            if (i != (int)bitmap.size()-1)
             {
                 cout << num->second;
             }
             else
             {
                 cout << num->second << endl;
             }
             i++;
         }


    }
}

 

posted @ 2014-07-15 11:09  丁香树  阅读(158)  评论(0编辑  收藏  举报