0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【python】カンガルーの位置を比較するプログラム。

Last updated at Posted at 2020-08-03

#【python】カンガルーの位置を比較するプログラム。(while文)

自分用メモです。

▼設問

  • 2匹のカンガルーが一定の距離間でジャンプしていく。
  • スタート地点は各々、x1, x2
  • 二回目以降のジャンプ距離は、v1, v2
  • 何回目かのジャンプで二匹が同じ位置にくる場合は”YES”をかえす。そうでない場合は、”NO”をかえす。

image.png

URL

▼sample input

x1 = 43
v1 = 2
x2 = 70
v2 = 2

▼sample output

'NO'

▼my answer

def kangaroo(x1, v1, x2, v2):
    n = 0
    condition = True
    while condition:
        n += 1
        c1 = x1 + v1*n
        c2 = x2 + v2*n

        #永遠に追いつかない
        if x1 <= x2 and v1 < v2:
            return "NO"
        elif x1 < x2 and v1 == v2:
            return "NO"
        elif x2 <= x1 and v2 > v1:
            return "NO"
        elif x1 > x2 and v1 == v2:
            return "NO"

        #追いつく可能性あり
        elif x1 <= x2 and v1 > v2:
            if c1 == c2:
                return "YES"
            elif c1 > c2:
                return "NO"
        elif x2 <= x1 and v2 > v1:
            if c1 == c2:
                return "YES"
            elif c2 > c1:
                return "NO"

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    x1V1X2V2 = input().split()

    x1 = int(x1V1X2V2[0])

    v1 = int(x1V1X2V2[1])

    x2 = int(x1V1X2V2[2])

    v2 = int(x1V1X2V2[3])

    result = kangaroo(x1, v1, x2, v2)

    fptr.write(result + '\n')

    fptr.close()

**・choreographing** 振り付けの You are choreographing a circus show with various animals. 動物振り付けサーカスショー

・while

while 条件式:
   処理

・条件式がTrueの間、処理を繰り返す。
・通常、Falseとなる処理を入れる。
・return終了させる場合、Falseになる処理は不要。

0
0
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?