4-25 可重复最优组合分解问题
问题描述
对于任意正整数 m,它的取 2 组合数定义为
设 n 是一个正整数,现在要求将 n 分解为若干个自然数的和,且使这些自然数的取 2 组合数的乘积最大。
对于给定的正整数 n,编程计算最优分解方案的最大取 2 组合数乘积的末尾有多少个 0。
数据输入:
第 1 行是正整数 n。
Java
import java.util.Scanner;
public class KeChongFuZuiYouZuHeFenJie {
private static int n;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
while (true){
n = input.nextInt();
compute();
}
}
private static void compute(){
if(n<8 && n!=5) {System.out.println(0); return;}
int r = n%5;
if(r == 0) System.out.println(n/5);
if(r == 1) System.out.println((n-6)/5);
if(r == 2) System.out.println((n-12)/5);
if(r == 3) System.out.println((n-8)/5);
if(r == 4) System.out.println((n-4)/5);
}
}
Input & Output
5
1
10
5
7
8
123
12345
56789
9999999
3456789
987654321
0
2
1
0
0
23
2469
11357
1999999
691357
197530863
0
Reference
王晓东《计算机算法设计与分析》