进制转换类型

1.反序数代码

package com.yt.baseconversion;

import java.util.Scanner;

//反序数代码
public class InverseNum {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int ans = 0;
        //方式1
        /*
        int res = 0;
        while (n > 0){
            ans = n % 10;
            n = n / 10;
            res = ans + 10*res;
        }
        System.out.println(res);

         */
        //方式二
        while (n>0){
            ans = 10 * ans;
            ans = ans + (n % 10);
            n = n / 10;
        }
        System.out.println(ans);
    }
}

2.10进制转x进制代码(x<10)

package com.yt.baseconversion;

import java.util.Scanner;

public class DecToX {
    public static void main(String[] args) {
        //十进制转换为x进制,其中x小于10
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();//输入需要转换的十进制数
        int x = scanner.nextInt();//输入需要转换为x进制的数,x<=10
        int ans[] = new int[105];//将转换的每一位数字保存到数组中
        int index = 0;//保存数组的下标
        while (n>0){//将十进制数逐位分解
            int w = n % x;
//            System.out.println(w);
            ans[index++] = w;
            n = n / x;
        }
        //注意:要反向输出数组的值才是正确的顺序
        for (int i = index-1; i >= 0; i--) {
            System.out.print(ans[i]);
        }
    }
}

3.10进制转x进制代码(通用版)

package com.yt.baseconversion;

import java.util.Scanner;

public class DecToGeneral {
    public static void main(String[] args) {
        //十进制转换为x进制,通用版
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();//输入需要转换的十进制数
        int x = scanner.nextInt();//输入需要转换为x进制的数
        int ans[] = new int[105];//将转换的每一位数字保存到数组中
        int index = 0;//保存数组的下标
        while (n>0){//将十进制数逐位分解
            int w = n % x;
//            System.out.println(w);
            if (w < 10){
                ans[index++] = w + '0';//变成字符需要加‘0’
            } else {
                //如果大于10则从A字符开始
                ans[index++] = (w - 10) + 'A';//如果要求转换为小写的,则需要加'a'
            }
            n = n / x;
        }
        //反序输出
        for (int i = index - 1; i >= 0; i--) {
            System.out.printf("%c" ,ans[i]);
        }
    }
}

4.x进制转10进制(x=2)

package com.yt.baseconversion;

import java.util.Scanner;

//实现二进制数转换为十进制
public class BiToDec {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String x = scanner.next();//输入一个二进制字符串
        int len = x.length();
        int res = 0;
        for (int i = 0; i < len; i++) {
            if (x.charAt(i) == '0'){//表示能被2整除
                res = res * 2;
            } else {
                res = res * 2 + 1;//表示被2除之后余数是1
            }
        }
        System.out.println(res);
    }
}

5.x进制转10进制(通用版)

package com.yt.baseconversion;


import java.util.Scanner;

//x进制转换为10进制,通用版
public class XToDec {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String x = scanner.next();//输入一个x进制字符串
        int n = scanner.nextInt();//说明输入的x是几进制
        int len = x.length();
        int res = 0;
        for (int i = 0; i < len; i++) {
            res = res * n;
            if (x.charAt(i) >= '0' && x.charAt(i) <= '9'){//表示一位数的数
                res = res + ((x.charAt(i) - '0') + 0);//转换为数字,res加一个与字符‘0’的差值
            } else {
                res = res + ((x.charAt(i) - 'A') + 10);
            }
        }
        System.out.println(res);
    }
}

6.x进制转y进制(通用版)

package com.yt.baseconversion;

import java.util.Scanner;

/*
小结:数的拆解和数的合并
1.拆解:先取模,后除取整
2.合并:先乘后加
 */
public class XToY {
    public static void main(String[] args) {
        //x进制转换为y进制(通用版)
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要转换的进制字符串");
        String s = scanner.next();
        System.out.println("字符串表示的进制数");
        int x = scanner.nextInt();
        System.out.println("需要转换为多少机制的数");
        int y = scanner.nextInt();
        //先将输入的字符串转换为十进制,再将十进制转换为相应的进制数
        int res = 0;//用来保存转换的十进制数
        for (int i = 0; i < s.length(); i++) {
            res = res * x;
            if (s.charAt(i)>= '0' && s.charAt(i)<= '9'){
                res = res + ((s.charAt(i) - '0') + 0);
            } else {
                res = res + ((s.charAt(i) - 'A') + 10);
            }
        }
//        System.out.println(res);
        //res中保存的是一个十进制数,将其转换为y进制的数
        //需要新的数组来保存转换的进制的每一位数
        int ans[] = new int[105];
        int index = 0;//用来保存数组的下标
        while (res>0){
            int w = res % y;
            if (w < 10) {
                ans[index++] = (w - 0) + '0';
            } else {
                ans[index++] = (w - 10) + 'A';
            }
            res = res / y;
        }
        //输出转换后的结果
        for (int i = index - 1; i >= 0 ; i--) {
            System.out.printf("%c", ans[i]);
        }

    }
}

7.题目 1572:蓝桥杯算法提高VIP-进制转换

题目描述

程序提示用户输入三个字符,每个字符取值范围是0-9,A-F。然后程序会把这三个字符转化为相应的十六进制整数,并分别以十六进制,十进制,八进制输出。

输入格式

输入只有一行,即三个字符。

输出格式

三个整数,中间用空格隔开。

样例输入

FFF

样例输出

FFF 4095 7777

题目来源蓝桥杯算法提高VIP-进制转换 - C语言网 (dotcpp.com)

代码实现

package com.yt.baseconversion;

import java.util.Scanner;

public class Test1572 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        //Integer.valueOf(String,radix)
        // 返回保持指定的String的值的Integer对象
        //radix指定返回的进制数
        int integer = Integer.valueOf(str,16);//str为十六进制。转换为十进制数值
        System.out.print(Integer.toString(integer,16).toUpperCase() + " ");
        System.out.print(integer + " ");
        System.out.print(Integer.toString(integer,8));//转换为八进制的字符串
    }
}

猜你喜欢

转载自blog.csdn.net/m0_57385165/article/details/129173814
今日推荐