【每日一题1124】乌龟赛跑

:woman_mage: 有两只小乌龟A和B,他俩谁也不服谁,就想比赛谁跑得快。A按照720米的均速运动,乌龟B知道它的速度更快,能跑850米每小时,但是手头的卷心菜还没吃完。当乌龟B出发的时候,已经发现乌龟A领先了70米。那乌龟B需要多久才能赶上乌龟A呢?

我们设定乌龟A的速度为v1,乌龟B的速度为v2,乌龟A领先的距离设为g,请编写一个函数solution(v1, v2, g),计算出他们追平时所需的时间,用一个包含3个元素的元组表示,元素分别代表时、分、秒数。

示例:
输入:v1=720, v2=850, g=70,返回:(0, 32, 18)

题目难度:中等
题目来源:codewars: Tortoise racing

def solution(v1: int, v2: int, g: int) -> tuple:
    # your code

assert solution(720, 850, 70) == (0, 32, 18)
assert solution(80, 91, 37) == (3, 21, 49)
def solution(v1: int, v2: int, g: int) -> tuple:
    g1 = g
    g2 = 0
    i = 0
    while True:
        g1 = v1 / 3600 + g1
        g2 = v2 / 3600 + g2
        if g2 >= g1:
            break
        i += 1
    return i // 3600, i % 3600 // 60, i % 60


assert solution(720, 850, 70) == (0, 32, 18)
assert solution(80, 91, 37) == (3, 21, 49)
def solution(v1: int, v2: int, g: int) -> tuple:
    tmp = v2-v1
    hour = g//tmp
    minute = g%tmp*60//tmp
    second = g%tmp*60%tmp*60//tmp
    return (hour,minute,second)

assert solution(720, 850, 70) == (0, 32, 18)
assert solution(80, 91, 37) == (3, 21, 49)
def solution(v1: int, v2: int, g: int) -> tuple:
    time=g*3600/(v2-v1)
    s=time%60
    m=time//60%60
    h=time//3600
    return (int(h),int(m),int(s))
assert solution(720, 850, 70) == (0, 32, 18)
assert solution(80, 91, 37) == (3, 21, 49)

def solution_1125(v1, v2, g):
‘’’
龟兔赛跑
我们设定乌龟A的速度为v1,乌龟B的速度为v2,乌龟A领先的距离设为g,请编写一个函数solution(v1, v2, g),计算出他们追平时所需的时间,用一个包含3个元素的元组表示,元素分别代表时、分、秒数。
:return:
‘’’
if v1 >= v2:
return (0, 0, 0)

need_time = g/(v2-v1)

time_hour = int(need_time)
time_min = int((need_time - time_hour)*60)
time_sec = int(((need_time-time_hour)*60 - time_min)*60)
return (time_hour,time_min, time_sec)

def test_solution_1125():
assert solution_1125(720,850,70) == (0, 32, 18)
assert solution_1125(80, 91, 37) == (3, 21, 49)

def solution(v1: int, v2: int, g: int) → tuple:
time = int(g * 3600 / (v2 - v1))
s = time % 60
m = time // 60 % 60
h = time // 3600 % 60
return h, m, s

assert solution(720, 850, 70) == (0, 32, 18)
assert solution(80, 91, 37) == (3, 21, 49)

def solution(v1: int, v2: int, g: int) → tuple:
time = g / (v2 - v1)
hour = int(time)
minutes = int((time - hour) * 3600 // 60)
second = int((time * 3600) % 60)
return hour, minutes, second

assert solution(720, 850, 70) == (0, 32, 18)
assert solution(80, 91, 37) == (3, 21, 49)

 public ArrayList<Integer> solution(int v1, int v2, float g) {
        float ga = g;
        float gb = 0f;
        int step = 1;
        DecimalFormat decimalFormat = new DecimalFormat("0.000000");
        while (true) {
            ga = Float.parseFloat(decimalFormat.format((float)v1 / 3600)) + ga;
            gb = Float.parseFloat(decimalFormat.format((float)v2 / 3600)) + gb;
            if (gb >= ga) {
                break;
            }
            step++;
        }
        ArrayList<Integer> res = new ArrayList<>();
        res.add(step / 3600);
        res.add(step % 3600 / 60);
        res.add(step % 60);
        return res;
    }
import math
def solution(v1: int, v2: int, g: int) -> tuple:
    ts = math.floor(3600 * g / (v2 - v1))
    th = math.floor(ts/3600)
    tm = math.floor((ts-th*3600)/60)
    tss = ts - th*3600 - tm * 60
    return (th,tm,tss)
def solution(v1: int, v2: int, g: int) -> tuple:
    time = g / (v2 - v1)
    hour = int(time)
    mimute = int((time - hour) * 60)
    second = int((((time - hour) * 60) - mimute) * 60)
    return (hour, mimute, second)


assert solution(720, 850, 70) == (0, 32, 18)
assert solution(80, 91, 37) == (3, 21, 49)