仅在Java中使用数组计算阶乘50 [英] Calculate factorial of 50 using array only in java

查看:89
本文介绍了仅在Java中使用数组计算阶乘50的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的java总初学者。
我有一门功课编写计算的50使用数组的阶乘一个完整的程序。
我不能使用类似的BigInteger任何方法。
因为我的教授希望我们明白背后的逻辑,我只能用数组,我猜...
不过,他并没有真正教我们数组的细节,所以我真的很困惑在这里。

I'm a total beginner of java. I have a homework to write a complete program that calculates the factorial of 50 using array. I can't use any method like biginteger. I can only use array because my professor wants us to understand the logic behind, I guess... However, he didn't really teach us the detail of array, so I'm really confused here.

基本上,我试图分裂大数目,放入数组插槽。因此,如果第一个数组得到235,我可以把它并提取号码,并把它变成一个阵列插槽。然后,将保持一个阵列插槽。重复这个过程,直到我得到的结果(这是50阶乘,这是一个巨大的数字。)

Basically, I'm trying to divide the big number and put it into array slot. So if the first array gets 235, I can divide it and extract the number and put it into one array slot. Then, put the remain next array slot. And repeat the process until I get the result (which is factorial of 50, and it's a huge number..)

我试图了解什么是背后的逻辑,但我真的无法弄清楚..到目前为止,我有这个在我的脑海。

I tried to understand what's the logic behind, but I really can't figure it out.. So far I have this on my mind.

import java.util.Scanner;
class Factorial
{
    public static void main(String[] args)
    {
        int n;
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter n");
        n = kb.nextInt();
        System.out.println(n +"! = " + fact(n));
    }

    public static int fact(int n)
    {
        int product = 1;
        int[] a = new int[100];
        a[0] = 1;



        for (int j = 2; j < a.length; j++)
        {
            for(; n >= 1; n--)
            {
                product = product * n;

                a[j-1] = n;
                a[j] = a[j]/10;
                a[j+1] = a[j]%10;

            }

        }
        return product;
    }
}

不过,这并不表明我的50阶乘。
它显示我作为0的结果,因此,很显然,它不工作。

But it doesn't show me the factorial of 50. it shows me 0 as the result, so apparently, it's not working.

我试图使用一种方法(实际上()),但我不知道这是应该做的正确方法。
我的教授提到的有关使用操作符/和%反复分配给数组的下一个插槽的数量。
所以我尝试使用此作业。

I'm trying to use one method (fact()), but I'm not sure that's the right way to do. My professor mentioned about using operator / and % to assign the number to the next slot of array repeatedly. So I'm trying to use that for this homework.

有没有人有这个功课的想法?
请帮帮我!

Does anyone have an idea for this homework? Please help me!

而对于混乱的指令对不起...我很困惑也,所以请大家见谅。

And sorry for the confusing instruction... I'm confused also, so please forgive me.

FYI:50阶乘是30414093201713378043612608166064768844377641568960512000000000000

FYI: factorial of 50 is 30414093201713378043612608166064768844377641568960512000000000000

推荐答案

试试这个。

static int[] fact(int n) {
    int[] r = new int[100];
    r[0] = 1;
    for (int i = 1; i <= n; ++i) {
        int carry = 0;
        for (int j = 0; j < r.length; ++j) {
            int x = r[j] * i + carry;
            r[j] = x % 10;
            carry = x / 10;
        }
    }
    return r;
}

int[] result = fact(50);
int i = result.length - 1;
while (i > 0 && result[i] == 0)
    --i;
while (i >= 0)
    System.out.print(result[i--]);
System.out.println();
// -> 30414093201713378043612608166064768844377641568960512000000000000

这篇关于仅在Java中使用数组计算阶乘50的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆