575. 进制转化

题目描述

给定一个十进制正整数,要求输出这个正整数的二进制数、八进制数和十六进制数。

解答要求时间限制:1000ms, 内存限制:100MB
输入

输入数据只有一行,包含一个十进制正整数x(1 <= x <= 1000000000)

输出

输出一共3行,第一行输出二进制数,第二行输出八进制数,第三行输出十六进制数,十六进制数中的字母采用大写字母。

样例

输入样例 1 复制

13

输出样例 1

1101
15
D
提示样例 1
 代码:
// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.
void Trans_two(int N)
{
    int a[1005],i = 0;
    while(N>0)
    {
        int t = N%2;
        N = N/2;
        a[i] = t;
        i++;
    }
    for (int k = i-1;k>=0;k--)
    cout<<a[k];
    cout<<endl;
}
void Trans_eight(int N)
{
    int a[1005],i = 0;
    while(N>0)
    {
        int t = N%8;
        N = N/8;
        a[i] = t;
        i++;
    }
    for (int k = i-1;k>=0;k--)
    cout<<a[k];
    cout<<endl;
}
void Trans_sixteen(int N)
{
    char arr []= "0123456789ABCDEF";
    char hex[16];
    int i = 0;
    while(N>0)
    {
        hex[i++] = arr[N%16];
        N = N/16;
    }
    for (int k = i-1;k>=0;--k)
    cout<<hex[k];
    cout<<endl;
}
int main()
{  
  // please define the C++ input here. For example: int a,b; cin>>a>>b;;  
  // please finish the function body here.  
  // please define the C++ output here. For example:cout<<____<<endl; 
    int N;
    cin>>N;
    Trans_two(N);
    Trans_eight(N);
    Trans_sixteen(N);
  return 0;
}
以大多数人努力程度之低,根本轮不到去拼天赋~
原文地址:https://www.cnblogs.com/gcter/p/15472047.html