- 按键认证大神
- 2699998
- 3587
- 11
- 2173 朵
- 7386 个
- 1021 个
- 91120
- 2014-08-23
|
1#
t
T
发表于 2022-07-22 00:10
|
|只看楼主
题目描述 给了一些木头。它由一个整数数组woods (参数1)给出,每个元素是木头的长度。将它们切成小块,以保证您可以拥有等于或多于n (参数 2)具有相同长度的块(长度应该是整数)。你的任务是找出你能从这些树林中得到的最长长度是多少?返回小块的最大长度。
一个例子: woods = [232, 124, 456], n = 7 我们拥有的木头长度分别是232,124,456,需要裁成7份同长度的木头,要求尽可能的长,结果应该是114,因为 232 可以裁成2段114长度的木头,剩余4(可以丢弃),124可以裁一段剩余10,456可以裁成4段无剩余。裁出来的数量是 2+1+4 = 7段,如果我们裁成115则最多只能裁出6段一样长的。
注意: 中的所有数字woods 都是正整数; n 也是一个正整数; 结果应该是整数长度的小块,尽可能长; 请注意优化代码避免超时
题目难度:一般 题目来源:Wood Cut | Codewars 题目交流: 584781753
|
- Import "SmAssert.dll"
- Function 裁木料(数组, 等份)
- // 您的代码写在这里
- End Function
- SmAssert 裁木料(Array(232, 124, 456), 7) = 114
- SmAssert 裁木料(Array(232, 124, 456), 20) = 38
- SmAssert 裁木料(Array(232, 124, 456), 1) = 456
- SmAssert 裁木料(Array(232, 124, 456), 2) = 232
- SmAssert 裁木料(Array(232, 124, 456), 3) = 228
- SmAssert 裁木料(Array(1, 1, 1), 4) = 0
- SmAssert 裁木料(Array(1, 1, 1), 3) = 1
- SmAssert 裁木料(Array(739,236,932,583,524,983,682,55,679,582,283,439,405,245,627,819,452,540,412,919,444,589,102,104,937,940,484,804,522,49,745,435,17,26,918,423,790,27,884,655,651,799,786,71,503,910,461,804,520,661,706,953,144,878,847,663,476,230,848,258,500,907,45,345,221,934,562,428,459,320,894,944,727,294,786,451,888,266,362,878,950,690,900,810,953,433,181,267,370,874), 45) = 582
复制代码 参考题解- Import "SmAssert.dll"
- VBSBegin
- ' 求最大值
- Function Max(Arr)
- Dim i, result
- For i = 0 To UBound(Arr)
- If Arr(i) > result Then
- result = Arr(i)
- End If
- Next
- Max = result
- End Function
- VBSEnd
- Function 裁木料(数组, 等份)
-
- '【作者】:神梦无痕
- '【QQ】:1042207232
- '【Q群】:584781753
-
- Dim maxx, minn, m, num, i
-
- maxx = Eval(Join(数组, "+"))
- minn = Max(数组) \ 等份
- Do While (minn <= maxx)
- m = (minn + maxx) \ 2
- If m = 0 Then m = 0 : Exit Do
- num = 0
- For i = 0 To UBound(数组)
- num = num + (数组(i) \ m)
- Next
- If num >= 等份 Then
- minn = m + 1
- Else
- maxx = m - 1
- m = m - 1
- End If
- Loop
- 裁木料 = m
- End Function
- SmAssert 裁木料(Array(232, 124, 456), 7) = 114
- SmAssert 裁木料(Array(232, 124, 456), 20) = 38
- SmAssert 裁木料(Array(232, 124, 456), 1) = 456
- SmAssert 裁木料(Array(232, 124, 456), 2) = 232
- SmAssert 裁木料(Array(232, 124, 456), 3) = 228
- SmAssert 裁木料(Array(1, 1, 1), 4) = 0
- SmAssert 裁木料(Array(1, 1, 1), 3) = 1
- SmAssert 裁木料(Array(739,236,932,583,524,983,682,55,679,582,283,439,405,245,627,819,452,540,412,919,444,589,102,104,937,940,484,804,522,49,745,435,17,26,918,423,790,27,884,655,651,799,786,71,503,910,461,804,520,661,706,953,144,878,847,663,476,230,848,258,500,907,45,345,221,934,562,428,459,320,894,944,727,294,786,451,888,266,362,878,950,690,900,810,953,433,181,267,370,874), 45) = 582
- ' 按键精灵带不动下面的例子
- // SmAssert 裁木料(Array(200000000, 2147483645, 2147483646, 2147483647), 4) = 1073741823
- // SmAssert 裁木料(Array(2000000000, 2147483645, 2147483646, 2147483647), 4) = 2000000000
复制代码 插件下载【插件】神梦断言插件 SmAssert.dll,帮助开发者发现业务逻辑错误
|