本文共 974 字,大约阅读时间需要 3 分钟。
题目地址:
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.
For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
Note: You may assume that n is not less than 2 and not larger than 58.
这种题目就找规律吧,从2开始:
于是代码可以这么写:
public class IntegerBreak { public static int integerBreak(int n) { if (n == 2) return 1; if (n == 3) return 2; int res = 1; while (n > 4) { res *= 3; n -= 3; } return res * n; } public static void main(String[] args) { for (int i = 1; i < 58; i++) { System.out.println(i + " " + integerBreak(i)); } }}
转载地址:http://fihii.baihongyu.com/