题解 | #最大的奇约数#
最大的奇约数
https://www.nowcoder.com/practice/49cb3d0b28954deca7565b8db92c5296
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static long jishu(long n) { long s1 = 0; if (n == 1) { return s1; } else { s1 = (1 + n) * (n + 1) / 4; return s1; } } public static long oushu(long n) { long s = 0; if(n == 2){ return 1; } else if(n == 4){ return 2; } else { if(n%4 == 0) { s = jishu(n/2-1) + oushu(n/2); } else{ s = jishu(n/2) + oushu((n/2)-1); } return s; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); long sum1 = 0; long sum2 = 0; if(N == 1){ System.out.println(1); return; } else if(N ==2){ System.out.println(2); return; } if(N%2 != 0){ sum1 = jishu(N) + oushu(N-1); } else{ sum1 = jishu(N-1) + oushu(N); } System.out.println(sum1); } }
感觉写的很绕