我尝试一次用 n 个楼梯和 1 或 2 或 3 个步骤来解决楼梯问题,但我的解决方案一直运行良好,直到 13 点之后才没有,它给出的更少

I tryed to do stairCase problem with n stairs and 1or2or3 steps at a time But my solution is working fine until 13 after that not, its giving less

public class StairCase {
    public static int Solution(int n) {
        int sum, count = 0;
//Funtion to calculate stair case and use factorial because a solution can have many ways.
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                for (int k = 0; k <= n; k++) {
                    sum = i + (j * 2) + (k * 3);
                    if (sum == n) {
                        count = count + (fact.fact1(i + j + k) / (fact.fact1(i) * fact.fact1(j) * fact.fact1(k)));
                    }
                }
            }

/*这个函数背后的逻辑是,就像我们必须找到没有排列 abbbccde 一样,我们可以写所以我们写 (8!/(3!*2!)) 所以在得到 ijk= 1,2,3 之后,我以那种方式重新排列了它们 (i+j+k)!/i!*j!k! !=阶乘/

        }
        return count;
    }

    public static void doTestPass() {

        boolean result = true;
        result = result && (Solution(3) == 4);
        result = result && (Solution(4) == 7);
        result = result && (Solution(11) == 504);
        result = result && (Solution(12) == 927);
        result = result && (Solution(13) == 1705);
//working fine till 13 not after that
        System.out.println(Solution(14));
        System.out.println(Solution(20));
        System.out.println(Solution(21));

//14--3127 20--68603 21--94351(orignal ans-- 3136,121415,223317)

        if (result) {
            System.out.println("All Test Cases Passed");
        } else {
            System.out.println("Test Case Failed");
        }
    }

    public static void main(String[] Args) {
        doTestPass();
    }

public class 事实

{

 static int fact1(int n)

{

//这个函数是创建阶乘

    int res = 1;

    for (int i = 2; i <= n; i++)

{

        res = res * i;

}

   return res;

}

}

}

问题与在阶乘方法中使用原始整数并溢出 2,147,483,647 的整数限制有关。

在下面查看阶乘代码的试运行:

fact1(12) 输出 47900160012!

是正确的

fact1(13) 输出 1932053504 而不是 13! 应该6227020800

这意味着当您在代码中执行 fact.fact1(i) 时,对于任何大于 12 的值,它都会输出错误的答案。这将需要您重新设计用于将这些大数字保存为 BigInteger.

的数据结构

修改后的 fact 方法如下所示:

BigInteger fact1(int n) {
    // this funtion is to create factorial
    BigInteger res = BigInteger.ONE;
    for (int i = 2; i <= n; i++) {
        res = res.multiply(BigInteger.valueOf(i));
    }
    return res;
}

现在 fact1(13) 的输出是 6227020800 的正确值。

您现在需要使用 BigInteger 重新编写代码的其他方面,但您现在应该了解您的问题所在。