Blue Bridge Cup questions Tortoise and the Hare basic exercises forecasting Python and C ++

Basic exercises forecasting questions Tortoise and the Hare

By submitting this question  

Resource constraints

Time limit: 1.0s Memory Limit: 512.0MB

Problem Description

  Saying that there are a variety of hare and the tortoise in the world, but the study found that all of the hare and the tortoise have one thing in common - like race. So every corner of the world are constantly undergoing tortoise and hare race, Xiaohua was interested, and decided to study different racing hare and the tortoise's. He found that, although the rabbit run faster than the tortoise, but they have well-known problems - the pride and laziness, so in the game with turtle, once any one second after the end of the rabbit found himself leading t m or more, they will stop s seconds rest. For different rabbits, t, the value s is different, but all are consistent tortoise - no end they never stop.
  However, some very long game, watch the whole will spend a lot of time, and Xiaohua found that as long as the record hare and the tortoise after each game starts data - rabbit speed v1 (v1 represents the rabbit run meters per second), turtle speed V2, and the corresponding rabbit t, s value, and the track length l-- can predict the outcome of the game. But Xiaohua lazy and do not want to guess the outcome of the race by hand calculations, he found you - a brilliant student at Tsinghua University Department of Computer Science - ask for help, you write a program for a game input data v1, v2, t, s, l, of the predicted game result.

Input Format

  Only input line, comprising a space separated five positive integer v1, v2, t, s, l, wherein (v1, v2 <= 100; t <= 300; s <= 10; l <= 10000 and is v1 , v2 common multiple)

Output Format

  Comprising two output lines, the first line of output results of the competition - a capital letter "T" or "R" or "D", respectively tortoise wins, the winning rabbit, or both reach the end.
  The second line outputs a positive integer indicating the end of the time taken (in seconds) winner (or both simultaneously) reaches.

Sample input

10 5 5 2 20

Sample Output

D
4

Sample input

10 5 5 1 20

Sample Output

R
3

Sample input

10 5 5 3 20

Sample Output

T
4

Python version

v1, v2, t, s, l = map(int, input().split())
time, s1, s2 = 0, 0, 0
while s2 < l and s1 < l:
    s1 += v1
    s2 += v2
    time += 1
    if s1 >= l or s2 >= l:
        break
    if s1 - s2 >= t:
        s1 -= s * v1
if s1 == s2:
    print("D")
elif s1 > s2:
    print("R")
else:
    print("T")
print(time)

C ++ version

#include<stdio.h>
int main()
{
    int v1,v2,t,s,l,l1=0,l2=0,i=0;
    scanf("%d%d%d%d%d",&v1,&v2,&t,&s,&l);
    while(l1<l && l2<l)
    {
        l1=l1+v1;
        l2=l2+v2;
        i++;
        if(l1>=l || l2>=l)break;
        if(l1-l2>=t)l1=l1-s*v1;
    }
    if(l1>l2)printf("R\n");
    else if(l2>l1)printf("T\n");
    else printf("D\n");
    printf("%d\n",i);
    return 0;
}

 

Published 74 original articles · won praise 139 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43906799/article/details/105015284