进制转换

来源:互联网 发布:淘宝卖假货封店 编辑:程序博客网 时间:2024/06/08 12:13

进制转换
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R != 10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Example Input

7 2
23 12
-4 3

Example Output

111
1B
-11

think:
此道题目其实并不困难,但却要仔细思考,同时也要掌握进制之间的转换关系。
十->(各个进制):
while(十进制数字>0)
{
十进制数字%要转换的进制——–得到余数(作为第一位……)
十进制数字/要转换的进制
}
注意:
还要考虑十六进制的两位数字的表现形式;
当数字为负数的时候,还要输出负号。

#include <bits/stdc++.h>using namespace std;int main(){    int a[121121];    long long int n, m;    while(cin>>n>>m)    {       if(n<0)       {          n = -n;          cout<<'-';       }       if(n==0)       cout<<0;       int top = 0;       while(n>0)       {         int b = n%m;         a[top++] = b;         n /= m;       }       top = top -1;       for(int j=top;j>=0;j--)       {          if(a[j]==10)          cout<<'A';          else if(a[j]==11)          cout<<'B';          else if(a[j]==12)          cout<<'C';          else if(a[j]==13)          cout<<'D';          else if(a[j]==14)          cout<<'E';          else if(a[j]==15)          cout<<'F';          else          cout<<a[j];       }       cout<<endl;    }    return 0;}
原创粉丝点击