Skip to content

Commit

Permalink
[algorithms]贪心算法拆分整数获取最大和的问题示例
Browse files Browse the repository at this point in the history
  • Loading branch information
Trackerming committed Jul 12, 2024
1 parent 1468c63 commit 5d80e71
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions algorithms/src/greed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ pub fn coin_change_greedy(coins: &mut [i32], mut amt: i32) -> i32 {
}
}

// 最大切分乘积问题
// 给定一个正整数n,将其切分为至少两个正整数的和,求切分后所有整数的乘积最大是多少
// 2*(n-2)>=n;这说明大于等于4的整数都应该被切分
// 如果切分方案中包含4的因子,那么它就应该被继续切分。最终的切分方案只应出现1、2、3这三种因子
// 剩下的就是比较是拆分几个3和几个2的问题
pub fn max_product_cutting(n: i32) -> i32 {
// 当 n <= 3 时,必须切分出一个 1
if n <= 3 {
return 1 * (n - 1);
}
// 贪心地切分出 3 ,a 为 3 的个数,b 为余数
let a = n / 3;
let b = n % 3;
if b == 1 {
// 当余数为 1 时,将一对 1 * 3 (1+3=4)转化为 2 * 2
3_i32.pow(a as u32 - 1) * 2 * 2
} else if b == 2 {
// 余数为2的适合,所以不做处理
3_i32.pow(a as u32) * 2
} else {
// 当余数为0的适合,不做处理,此时已经是最大的因子3自己运算了
3_i32.pow(a as u32)
}
}

#[cfg(test)]
mod greedy_test {
use super::*;
Expand All @@ -38,4 +63,18 @@ mod greedy_test {
let count = coin_change_greedy(&mut coins, 60);
assert_eq!(count, 11);
}

#[test]
fn test_max_product_cutting() {
let max_product = max_product_cutting(3);
assert_eq!(max_product, 2);
let max_product = max_product_cutting(6);
assert_eq!(max_product, 9);
// 3*2*2>3*3*1
let max_product = max_product_cutting(7);
assert_eq!(max_product, 12);
// 3*3*2
let max_product = max_product_cutting(8);
assert_eq!(max_product, 18);
}
}

0 comments on commit 5d80e71

Please sign in to comment.