1. Title Description
/ * Title Description give you a string of length n, m please cut the rope section (m, n are integers, n> 1 and m> 1), the length of each rope is referred to as k [0], k [1], ..., k [m]. Will k [0] xk [1] x ... xk [m] maximum possible product is how much? For example, when the length of the rope is 8:00, we cut it into lengths of three sections 2,3,3 of the product obtained at this time is the maximum 18. Input Description: Enter a number n, see the problem surface meaning. (2 <= n <= 60 ) Example 1 input 8 output 18 * /
Code 1: greedy algorithm (easiest)
Thinking
/ ** * Title Analysis: * first few examples, rules to be seen. * 4: 2 * 2 * 5: 2 * 3 * 6: 3 * 3 * 7: 2 * 2 * 3 or 4 * 3 * 8: 2 * 3 * 3 * 9: 3 * 3 * 3 * 10: 2 * 2 * 3 * 3 or 4 * 3 * 3 * 11: 2 * 3 * 3 * 3 * 12: 3 * 3 * 3 * 3 * 13: 2 * 2 * 3 * 3 * 3 or 4 * 3 * 3 3 * * * the following is the analysis: * first determine k [0] to k [m] which may be digital, in fact, can only be 2 or 3. * Of course, there may be 4, but 4 = 2 * 2, we simply those who do not consider them. * 5 <2 * 3,6 <3 * 3, larger than the number 6 we do not have even considered, we must continue to divide. * Second, look at the number 2 and 3, the number 2 is certainly less than three, why? Because 2 * 2 * 2 <3 * 3, then the title is simple. * 3 divided by n directly The remainder is judged from a two or 2 2 2 on the line or not. * Title predetermined Since m> 1, it is only 2 * 1 * 1 2 3 only, these two special cases directly back on the line. * * Complexity of computing power is: O (log n), with Dynamic Programming would be time-consuming to do more. * /
Let as many 3
Code
import java.util.*; public class Solution { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); cutRope(n); } public static int cutRope(int target) { if(target == 2){ return 1; } if(target == 3){ return 2; } int num3 = target/3; int num2 = 0; switch(target%3){ case 0:break; case 1:{ num3 = num3-1; num2 = 2; break; } case 2:{ num2 = 1; break; } } return (int) (Math.pow(2,num2)*Math.pow(3,num3)); } }
Code 2: Dynamic Programming
Ideas:
@ Dynamic programming: the product of the maximum available length i is: dp [i] = dp [j] * dp [ij] maximum
Import Classes in java.util *. ; public class Solution { public static void main (String [] args) { Scanner SC = new new Scanner (the System.in); int n-= sc.nextInt (); cutRope (n-); } // dynamic programming: the product of the maximum available length i is: dp [i] = dp [ j] * dp [ij] maximum public static int cutRope ( int n-) { // n-<= 3 case, m> 1 It must be segmented IF (n-== 2 ) return . 1 ; IF (n-==. 3 ) return2 ; int [] DP = new new int [n-+. 1]; // maximum product length i when the available DP [ . 1] =. 1 ; DP [ 2] = 2 ; DP [ . 3] =. 3 ; int RES = 0; // record the maximum for ( int I = 4; I <= n-; I ++) { // Note 4 as a boundary for ( int J =. 1; J <= I / 2; J ++ ) { // dynamic programming : the maximum available length of the product i: dp [i] = dp [ j] * dp [ij] maximum RES = Math.max (RES, DP [J] * DP [ij of ]); } DP [i]=res; } return dp[n]; } }