1、计算最小苹果个数
import java.util.Scanner; public class Main { public static int calcuAppleNum(int n) { double num = 0.0d; int intNum = 0; for (int i = 1;; i++) { num = fun(i, n, n); intNum = (int) num; if ((num - intNum) < 10e-9) break; } return intNum; } public static double fun(int min, double n, int index) { if (index <= 1) return 1 + n * min; return n / (n - 1) * fun(min, n, index - 1) + 1; } public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); s.close(); System.out.println(calcuAppleNum(n)); } }
2、计算高度
import java.util.Scanner; public class Main { public static int calcuSumHigh(int a[]){ if(a==null ||a.length <1) return 0; int sum = 0; for(int i=0;i<a.length;i++){ sum += fun(a[i]) - a[i]; } return sum; } public static int fun(int n){ if(n < 1) return 0; return 2*n+fun(n/2); } public static void main(String[] args) { Scanner s = new Scanner(System.in); int a[] = new int[4]; int i =0 ; while(i<4){ a[i++] = s.nextInt(); } s.close(); System.out.println(calcuSumHigh(a)); } }
时间: 2025-01-31 07:32:19