zoukankan      html  css  js  c++  java
  • 剪绳子

    题目:

    给你一段长度为n的绳子,请把绳子剪成m段,每段绳子的长度记为:k[0]、k[1]、...。请问它们的乘积最大值是多少?

    解答:

     1 public class Solution {
     2     public static void main(String[] args) {
     3         System.out.println(maxProductAfterCutting(5));
     4     }
     5 
     6     public static int maxProductAfterCutting(int length) {
     7         // 长度为2,只可能剪成长度为1的两段
     8         if(length < 2) {
     9             return 0;
    10         }
    11 
    12         if(length == 2) {
    13             return 1;
    14         }
    15 
    16         if(length == 3) {
    17             return 2;
    18         }
    19 
    20         int[] products = new int[length+1];
    21         products[0] = 0;
    22         products[1] = 1;
    23         products[2] = 2;
    24         products[3] = 3;
    25 
    26         int max = 0;
    27         for(int k = 4; k <= length; k++) {
    28             max = 0;
    29             for(int i = 1; i <= k/2; i++) {
    30                 
    31                 int product = products[i]*products[k-j];
    32                 if(product > max) {
    33                     max = product;
    34                 }
    35 
    36                 products[k] = max;
    37             }
    38         }
    39 
    40         return products[length];
    41 
    42     }
    43 }
  • 相关阅读:
    Maven ==> 简介
    IDEA结合GIT的使用
    Shell ==> 基础
    Dubbo ==> 简介
    iptables防火墙
    文件系统对比
    supervisord部署
    inotify+rsync安装配置
    前端插件网址
    Nginx高级玩法
  • 原文地址:https://www.cnblogs.com/wylwyl/p/10469359.html
Copyright © 2011-2022 走看看