【问题标题】:Convert a number from base 10 to N将一个以 10 为底的数字转换为 N
【发布时间】:2014-07-03 22:08:02
【问题描述】:

此代码是我必须开发的项目的一部分。我需要编写一个 java 方法,将一个数字(以 10 为基数)转换为 n 基数中的另一个数字。这是类的代码:

public class converter {

    public int from_10_to_n(int base, int newbase) {

       int k = 0;
       String res = "";
       String output = "";

       do {                        //Below I explain what I'm doing here
        base /= newbase;   
        k = base % newbase;
        res += String.valueOf(k);
       } while (base != 0);

       for(int i=res.length()-1; i>-1; i--) {
        output += res.charAt(i);
       }

       return Integer.parseInt(output);
    }

我想用这种方式制作程序:

do {} while(); 循环将数字相除并将余数保存在 k 中。然后我做了一个 for 循环来反转字符串 res(它有提醒)。

顺便说一句,当我在 main 中调用该方法时,我缺少最后一位数字。我的意思是:

converter k = new converter();
int e = k.from_10_to_n(a /*base 10 number*/, b /*Base n number*/);

System.out.println(a+" -> "+b+" is " + e);

使用此代码,如果我想将 231 转换为基数 4,则结果是 321 而不是 3213。我检查了我的代码,但找不到解决方案。有什么想法吗?

我对其他基地有同样的错误。例如 31(以 10 为底)是 11111(以 2 为底),但我的程序返回 1111

【问题讨论】:

  • 为什么不使用 java 的构建方式来做到这一点? Integer.toString(number, base)
  • 因为我想学会自己做。我知道那样,但它太容易了,我不能使用它(由于我正在从事的项目的规则)。
  • 请注意,您实际上并没有从 base 10 转换。从 base 10 字符序列到 Java int 的转换是由一些其他代码完成的,例如 Java 编译器或 Scanner.nextInt。您正在从 Java int 转换为 base n。
  • 是的,我知道,我在导入中使用 java.util.Scanner :)
  • 当您需要转换为大于 10 的基础 大于 时,乐趣就来了……但我不知道您是否需要它

标签: java


【解决方案1】:

翻转循环中前2行的顺序;通过先进行除法,您将失去第一个余数。但是你需要处理最后的剩余部分。

【讨论】:

  • 用 /= 交换 %= ?
  • @AlbertoRossi:不是%=。那应该还是%
  • 是的,我的意思是抱歉 :) 顺便说一句,现在它工作得很好,谢谢!
【解决方案2】:

问题出在这里:

base /= newbase;
k = base % newbase;

用一些实数试试这个,比如你的例子中的 231 和基数 4。

base /= newbase

现在 base 是 57,而您的 k 将不正确。你应该先得到余数,然后除:

k = base % newbase;
base /= newbase;

您的代码还存在一些样式问题,您应该考虑更正:

  • base 并没有真正的基础,只是输入值 - 也许将其重命名为 input 或类似的名称?
  • 混合缩进。您的循环缩进一个空格,而其余代码缩进四个。

【讨论】:

    【解决方案3】:

    我们十进制系统的字母是“0123456789”。 现在,以 N 为底的任意字母数字系统的字母表,大,是 “#0#1#2#...#9#10#11#...#1234#...#N” 因此,总结、简化、为输出添加易读性并进行测试:

    /*
     * Program  FromBase10ToBaseN
     * This program receives as input 
     * a positive integer number in base 10
     * and outputs its encoding in arbitrary base N.
     * The k-th symbol of the new base is denoted   #k.
     * Tests are provided.
    
     * Jose (2016)
     * evoljava.com 
    
     */
    
    
    package Programs;
    
    import java.util.Random;
    
    public class FromBase10ToBaseN {
    
    
        private static final Random RANDOM = new Random();
    
        public static void main(String[] args) throws Exception {
    
            //Number in base 10
            int inputValue = 9;
            //New base
            int N = 10;
            reportFromBase10ToBaseN(inputValue, N);
    
            inputValue = 100;
            N = 10;
            reportFromBase10ToBaseN(inputValue, N);
    
            inputValue = 8;
            //New base
            N = 2;
            reportFromBase10ToBaseN(inputValue, N);
    
            inputValue = 129;
            //New base
            N = 128;
            reportFromBase10ToBaseN(inputValue, N);
    
            inputValue = 127;
            //New base
            N = 128;
            reportFromBase10ToBaseN(inputValue, N);
    
    
    
            //test with random numbers
            for (int i = 0; i < 10; i++) {
                inputValue = RANDOM.nextInt(1000);
                //New base must be 2 or greater
                N = 2 + RANDOM.nextInt(80);
                reportFromBase10ToBaseN(inputValue, N);
            }
    
        }
    
    
        private static void reportFromBase10ToBaseN(int inputValue, int N) {
    
            String outputValue = fromBase10ToBaseN(inputValue, N);
            int Backwards = fromBaseNToBase10(outputValue, N);
            String isRight = "wrong";
            if (inputValue == Backwards) isRight = "right";
            System.out.println(inputValue + " in base 10 becomes "
                    + outputValue
                    + " in base " + N
                    + " Backwards: " + Backwards
                    + "   " + isRight);
        }
    
    
        private static String fromBase10ToBaseN(int inputValue, int N) {
            String res ;
            String outputValue = "";
    
            do {
    
                res = "#" + String.valueOf(inputValue % N);
                outputValue = res + outputValue;
                inputValue /= N;
    
            } while (inputValue != 0);
    
            return outputValue;
        }
    
    
    
        //The input string denotes a number in base newBase
        //The k-th symbol of the new base is denoted  #k.
        //Example:  #10#0 in base 15  represents 150 in base 10
        private static int fromBaseNToBase10(String inputValue, int N) {
    
            if (inputValue.charAt(0) == '#') {
                int outputValue = 0;
                boolean correct = true;
                int length = inputValue.length();
                String token = "";
                int power = 0;
                //Tokenizing:
                //Exmpl:  #1#120#13#2 is separated into tokens  1 120 13 2
                for (int i = length - 1; (i > -1) & correct; i--) {
                    char c = inputValue.charAt(i);
                    if (c != '#') {
                        token = c + token;
                    } else {
                        //tokens are evaluated
                        int p = Integer.parseInt(token);
                        //Detecting the presence of an alien symbol
                        if (p >= 0) {
                            outputValue 
                                = (int) (outputValue + p * Math.pow(N, power));
                            power++;
                        } else {
                            outputValue = -1;                      
                            correct = false;
                        }
                        token = "";
                    }
                }
    
                return outputValue;
            } else  return -1;
    
        }
    }//End of Program FromBase10ToBaseN
    
    
    OUTPUT:
    
    9 in base 10 becomes #9 in base 10 Backwards: 9   right
    100 in base 10 becomes #1#0#0 in base 10 Backwards: 100   right
    8 in base 10 becomes #1#0#0#0 in base 2 Backwards: 8   right
    129 in base 10 becomes #1#1 in base 128 Backwards: 129   right
    127 in base 10 becomes #127 in base 128 Backwards: 127   right
    382 in base 10 becomes #6#40 in base 57 Backwards: 382   right
    390 in base 10 becomes #11#5 in base 35 Backwards: 390   right
    788 in base 10 becomes #3#1#4 in base 16 Backwards: 788   right
    285 in base 10 becomes #3#4#6 in base 9 Backwards: 285   right
    68 in base 10 becomes #1#31 in base 37 Backwards: 68   right
    656 in base 10 becomes #24#8 in base 27 Backwards: 656   right
    442 in base 10 becomes #9#28 in base 46 Backwards: 442   right
    765 in base 10 becomes #21#30 in base 35 Backwards: 765   right
    455 in base 10 becomes #10#35 in base 42 Backwards: 455   right
    211 in base 10 becomes #4#3 in base 52 Backwards: 211   right
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      相关资源
      最近更新 更多