【每日一题20220719】三根等长的棍子

:mage:‍ 想象一下,给你两根棍子。你想得到三根等长的棍子。您可以切割其中一根或两根木棍来完成此操作,并且可以扔掉剩余的碎片。

编写一个函数 maxlen,它采用两根棍子的长度(L1 和 L2,均为正值),它将返回您可以制作三根棍子的最大长度。

题目难度:简单
题目来源:https://www.codewars.com/kata/57c1f22d8fbb9fd88700009b


def maxlen(num1: int, num2: int) -> Union[float, int]:
    # your code here

assert maxlen(5, 12) == 5
assert maxlen(12, 5) == 5
assert maxlen(5, 17) == 5.666666666666667
assert maxlen(7, 12) == 6.0
assert maxlen(25, 8) == 8.333333333333334
from typing import Union


def maxlen(num1: int, num2: int) -> Union[float, int]:
    # your code here
    try:
        list_num = [num1, num2]
        max_num = max(list_num)
        min_num = min(list_num)
        if max_num / 2 >= min_num:
            return min_num
        else:
            return max_num / 2
    except:
        pass


assert maxlen(5, 12) == 5
assert maxlen(12, 5) == 5
assert maxlen(5, 17) == 5.666666666666667
assert maxlen(7, 12) == 6.0
assert maxlen(25, 8) == 8.333333333333334

疑惑:请问断言(5,17)(25,8)这两处是否有错误,最后的结果已经大于其中一根棍子了

题目是没有错误的哦,仔细想想啥原因

from typing import Union
def maxlen(num1: int, num2: int) -> Union[float, int]:
    # your code here
    if num1 < num2:
        if num1 > num2/3 and num1 < num2/2:
            return num1
        elif num1 > num2/3 and num1 >= num2/2:
            return num2/2
        elif num1 <= num2/3:
            return num2/3
    else:
        return maxlen(num2,num1)

assert maxlen(5, 12) == 5
assert maxlen(12, 5) == 5
assert maxlen(5, 17) == 5.666666666666667
assert maxlen(7, 12) == 6.0
assert maxlen(25, 8) == 8.333333333333334
1 个赞

public double mexLen(int num1, int num2) {

        double avg = (num1 + num2) / 3.0;
        double len = 0.0;
        int maxLen = Math.max(num1, num2);
        int minLen = Math.min(num1, num2);
        if (num1 == num2) {
            len = num1 / 2.0;
        } else if (avg <= maxLen - avg && avg <= minLen - avg) {
            len = avg;
        }
        else if (maxLen / 3.0 < minLen && maxLen / 2.0 < minLen) {
            len = maxLen / 2.0;
        } else if (minLen < maxLen / 3.0) {
            len = maxLen / 3.0;
        }else if (minLen<= maxLen-minLen){
            len = minLen;
        }


            return len;
    }

    @Test
    void test1() {
        assertAll(
                () -> assertEquals(mexLen(5, 12), 5),
                () -> assertEquals(mexLen(12, 5), 5),
                () -> assertEquals(mexLen(5, 17), 5.666666666666667),
                () -> assertEquals(mexLen(7, 12), 6.0),
                () -> assertEquals(mexLen(7, 7), 3.5),
                () -> assertEquals(mexLen(25, 8), 8.333333333333334)
        );
    }

image

public static float maxLen2(float a,float b){
        if (a/b >= 3){
            return  a/3;
        }else if(a/b >= 2){
            return  b;
        }else if (a/b >= 1){
            return a/2;
        }
        return  maxLen2(b,a);
    }

代码块用三个 ` 符号框起来

我帮你修改了一下

谢谢,刚才好像会了 有个按钮</> 块引用。

def maxlen(num1: int, num2: int) -> Union[float, int]:
    if max((num1,num2)) - min((num1,num2))*2 > min((num1,num2)):
        return max((num1,num2)) / 3
    elif 0 < max((num1,num2)) - min((num1,num2))*2 < min((num1,num2)):
        return min((num1,num2))
    else:
        return max((num1,num2)) / 2

assert maxlen(5, 12) == 5
assert maxlen(12, 5) == 5
assert maxlen(5, 17) == 5.666666666666667
assert maxlen(7, 12) == 6.0
from typing import Union


def maxlen(num1:int, num2:int) -> Union[float, int]:
    if num1 < num2 :
       return maxlen(num2, num1)
    else:
        if num1 / num2 >= 2:
            return num2
        elif 1 <= num1 / num2 < 2:
            return (num1 - num2)
        else:
            return num1/2

assert maxlen(5, 12) == 5
assert maxlen(12, 5) == 5
assert maxlen(25, 8) == 8.3333333

def maxlen(num1: int, num2: int) -> Union[float, int]:
    max_ln=max(num1,num2)
    min_ln=min(num1,num2)
    if max_ln/3 >= min_ln:
        return max_ln/3
    elif  max_ln/2>= min_ln:
        return min_ln
    else:
        return max_ln/2
def maxlen(num1: int, num2: int) -> Union[float, int]:
    avg=(num1+num2)//3
    while True:
        if (num1//avg+num2//avg)>=3:
            if type(avg)==float:
                return avg+0.000000000000001
            else:
                return avg
        else:
            avg=round(max(num1/(num1//avg+1),num2/(num2//avg+1)),15)
            if type(avg)==float:
                avg-=0.000000000000001

assert maxlen(5, 12) == 5
assert maxlen(12, 5) == 5
assert maxlen(5, 17) == 5.666666666666667
assert maxlen(7, 12) == 6.0
assert maxlen(25, 8) == 8.333333333333334