【刷题】P94剑指offer:动态规划与贪婪算法:面试题14:剪绳子
发布日期:2021-05-12 12:42:15 浏览次数:10 分类:精选文章

本文共 2457 字,大约阅读时间需要 8 分钟。

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������

���������������������������������������������������������������������������������������������������������������������������������������������n���������������������������������������������������������������������������������

  • ���������������0���1������������������������������0���
  • ���������������2���������������������������������1���
  • ���������������3���������������������������������2���
  • ���������������������i������������������������������������������������������������������������������������������j���1���i/2������������f(j)���f(i-j)������������������������������f(i)���������
  • ������������������

    ������������������������������������������������������������������������������������������������������������

  • ������������������������3���������������3������������������
  • ���������������������4���������2���2���
  • ���������������������1���������������������������������1���������3���
  • ������������

    ���������������������������������������������������������������������������

    ������������

    int MaxProductAfterCutting_Solution(int length) {
    int[] product = new int[length + 1];
    product[0] = 0;
    product[1] = 1;
    product[2] = 2;
    product[3] = 3;
    int Max_Value = 0;
    for (int i = 4; i <= length; ++i) {
    Max_Value = 0;
    for (int j = 1; j <= i / 2; ++j) {
    int current = product[j] * product[i - j];
    if (current > Max_Value) {
    Max_Value = current;
    }
    }
    product[i] = Max_Value;
    }
    return product[length];
    }

    ������������

    int MaxProductAfterCutting_Solution(int length) {
    if (length <= 2) {
    return 0;
    }
    if (length == 3) {
    return 1;
    }
    int times_of_3 = length / 3;
    if ((length % 3) == 1) {
    times_of_3--;
    }
    int times_of_2 = (length - times_of_3 * 3) / 2;
    return (int)Math.pow(3, times_of_3) * (int)Math.pow(2, times_of_2);
    }

    ������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:【编程】C++入门:位运算(&、|、^、~、>>、<<)
    下一篇:【刷题】P89剑指offer:回溯法:面试题12:矩阵中的路径(详解)

    发表评论

    最新留言

    路过,博主的博客真漂亮。。
    [***.116.15.85]2025年04月08日 07时44分09秒