暴力代码
package com.lin.test0822.test3;
import java.util.Scanner;
//猴子爬山
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = n/ 3;
// System.out.println(k);
int result = 0;
for (int i = 0; i <= k; i++) {
int C_subscript = n-i*3+i;//下标
int C_superscript = i;//上标
// System.out.println("C_subscript:"+C_subscript+",C_superscript:"+C_superscript);
result += combination(C_superscript,C_subscript);
}
System.out.println(result);
}
public static int combination(int C_superscript,int C_subscript){
if (C_superscript == 0){
return 1;
}else {
long super_factorial = 1;
for (int i = 1; i < C_superscript+1; i++) {
super_factorial *= i;
}
long sub_factorial = 1;
for (int i = C_subscript; i > C_subscript-C_superscript ; i--) {
sub_factorial *= i;
}
return (int)(sub_factorial/super_factorial);
}
}
}