packagecom.test.util;publicclasstest{privatestaticvoidtest(doublex,doubley,intk,intn){y=y-y/k;if(y==(int)y){if(n==4){System.out.println("total:"+x+",k:"+k+",min:"+y);test...
package com.test.util;
public class test {
private static void test(double x, double y, int k, int n) {
y = y - y / k;
if (y == (int) y) {
if (n == 4) {
System.out.println("total:" + x + ",k:" + k + ",min:" + y);
test(x - 1, x - 1, k, 0);
} else {
test(x, y, k, n + 1);
}
} else {
test(x - 1, x - 1, k, 0);
}
}
public static void main(String[] args) {
test(10000, 10000, 5, 0);
}
}
自己写的一个小程序,不调整jvm的内存大小的情况下,也不改成迭代循环,堆栈溢出能解决么?
其实只是做一个小题:有一堆椰子,5个人,平均分5份,吃了1份,剩下4份再平均分为5份,以此类推,求分第5次时,每人多少个,用递归做。
private static void test(double x, double y, int k, int n) {
if (x < 1 || y < 1) {
return;
}
y = y - y / k;
if (y == (int) y) {
if (n == 4) {
System.out.println("total:" + x + ",k:" + k + ",min:" + y);
test(x - 1, x - 1, k, 0);
} else {
test(x, y, k, n + 1);
}
} else {
test(x - 1, x - 1, k, 0);
}
}
展开