简体   繁体   English

Java - 因子基本转换

[英]Java - Factorial Base Conversion

I am relatively new to programming, but I am trying to write a program that converts a number from base 10 to factorial base in order to assist me with a more challenging problem. 我对编程比较陌生,但我正在尝试编写一个程序,将一个数字从基数10转换为阶乘基数,以帮助我解决更具挑战性的问题。 Factorial base represents a number as the sum of factorials; 因子基数代表一个数作为阶乘的总和; for instance, 19 (base 10) = 301 (base factorial), because 3*3!+0*2!+1*1!=19. 例如,19(基数10)= 301(基因子),因为3 * 3!+ 0 * 2!+ 1 * 1!= 19。 You can see a better explanation than mine at http://en.wikipedia.org/wiki/Factorial_number_system . 您可以在http://en.wikipedia.org/wiki/Factorial_number_system上看到比我更好的解释。 What is the simplest way of accomplishing this for a number you can input? 对于您可以输入的数字,最简单的方法是什么? So far, I have been able to simply print the factorial of every number less than 10: 到目前为止,我已经能够简单地打印每个小于10的数字的阶乘:

for (int base = 1; base < 10; base++){
    int factorial = 1;
    for (int i = 1; i < base; i++){
        factorial = factorial*i;}
    System.out.println(factorial);}

I am sure I am making numerous beginner mistakes; 我相信我会犯很多初学者的错误; but I greatly appreciate any help you can give me for this. 但我非常感谢你能给我的任何帮助。 Thank you for your time, and for any assistance you can provide. 感谢您的时间,以及您可以提供的任何帮助。

for (int base = 1; base < 10; base++);{

You shouldn't have a semicolon between the for statement and the open brace. 你不应该在for语句和open括号之间加一个分号。 The compiler is interpreting it as if you meant 编译器将其解释为您的意思

for (...) { /* empty block */ }
{ // new, unrelated block
  ...
}

For the actual implementation, you can look to the Wikipedia article: 对于实际实现,您可以查看Wikipedia文章:

For instance, one can convert a number into factorial representation producing digits from right to left, by repeatedly dividing the number by the place values (1, 2, 3, ...), taking the remainder as digits, and continuing with the integer quotient, until this quotient becomes 0. 例如,可以通过将数字重复除以位置值(1,2,3,...),将余数作为数字,并继续整数,将数字转换为从右到左生成数字的阶乘表示。商,直到此商变为0。

So something like (untested, psuedocodey)... 所以像(未经测试,伪装)......

int number = 463;
int radix = 1;
String result = ""; // prob use int[] or list in real life?

while (number > 0) {
   int div = number / radix;
   int remainder = number % radix;
   result = remainder + result;
   number = div;
   ++radix;
}

Try this 尝试这个

public int convertToFactorial(int x10){
    int length = String.valueOf(x10).length();
        String[] vals = String.valueOf(x10).split("");

        int result = 0;
        for (int base = 1; base < length + 1; base++){
            int factorial = 1;
            for (int i = vals.length - base; i > 0; i--){
                factorial = factorial*i;
            }
            result += Integer.parseInt(vals[base]) * factorial;
        }
        System.out.println(result);
     return result;
}

Ps look at java code convention ant language structures, you shouldn't write ; ps看一下java代码约定的蚂蚁语言结构,你不应该写; after loop defenition. 循环后防御。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM