将数字从基数10转换为N.

时间:2014-07-03 22:08:03

标签: java

此代码是我必须开发的项目的一部分。我需要编写一个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);
    }

我想以这种方式制作节目:

enter image description here

do {} while();循环划分数字并将剩余部分保存在k中。然后我创建一个for循环来反转字符串res(它有提醒)。

顺便说一句,当我在我的主要方法中调用该方法时,我错过了最后一位数字。我的意思是:

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

3 个答案:

答案 0 :(得分:6)

翻转循环中前两行的顺序;通过先做分裂,你会失去第一个余数。但是你需要处理最后的剩余部分。

答案 1 :(得分:3)

问题在于:

base /= newbase;
k = base % newbase;

尝试使用一些实数,例如231和基数4。

base /= newbase

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

k = base % newbase;
base /= newbase;

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

  • base并不真正拥有任何基础,只是输入值 - 可能会将其重命名为input或类似的东西?
  • 混合缩进。你的循环用一个空格缩进,而其余的代码有四个。

答案 2 :(得分:0)

我们的十进制系统的字母表是" 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