zoukankan      html  css  js  c++  java
  • 十进制转化成八进制(一到十六进制)

    【问题描述】

    用递归算法,把任一给定的十进制正整数转换成八进制数输出。

    思路就是将该数不断除以8取余,直到商为0。再将所得余数倒着输出。到这输出就想到栈,栈就可以用递归实现。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<cstring>
     6 using namespace std;
     7 int n;
     8 void change(int x)
     9 {
    10     int r = x % 8;
    11     x /= 8;
    12     if(x != 0) change(x);
    13     printf("%d", r);
    14 }
    15 int main()
    16 {
    17     scanf("%d", &n);
    18     change(n);
    19     return 0;
    20 }

    以此类推,就可以转化成任意1到16进制的数。只不过当余数大于9时就要用 ABCDEF 代替。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<cstring>
     6 using namespace std;
     7 int n, m;
     8 char cha[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
     9 void change(int x, int y)
    10 {
    11     int r = x % y;
    12     x /= y;
    13     if(x != 0) change(x, y);
    14     if(r < 10) printf("%d", r);
    15     else printf("%c", cha[r - 10]);
    16 }
    17 int main()
    18 {
    19     scanf("%d%d", &n, &m);
    20     change(n, m);
    21     return 0;
    22 }

    输入:156 16   输出:9C

  • 相关阅读:
    Asp:Cookies应用指南
    asp:cookies的属性
    数据库压缩
    asp之servervariables全部显示
    sql语句操作表
    asp之FSO大全
    SQL语句
    vbscript语句
    asp之vbscript函数
    IDEA 2017web项目的创建
  • 原文地址:https://www.cnblogs.com/mrclr/p/8325692.html
Copyright © 2011-2022 走看看