0
2

More than 3 years have passed since last update.

【python】収穫できるリンゴとオレンジの数を求めるプログラム

Last updated at Posted at 2020-06-05

【python】収穫できるリンゴとオレンジの数を求めるプログラム

個人メモです。

設問

家の敷地内(座標s~t)に入ったリンゴの数をカウントする。

image.png

s:家の始点
t:家の終点
a:りんごの木の位置
b:オレンジの木の位置
apples:リンゴの落下地点(相対)
oranges:オレンジの落下地点(相対)

URL

▼sample input

s=2
t=3
a=1
b=5
apples=[-2,2,1]
oranges=[5,8,-2,-1]

▼sample output

2
1

▼my answer

def countApplesAndOranges(s, t, a, b, apples, oranges):
    yeiledApple = 0
    yeiledOrange = 0

    #apple
    for apple in apples:
        aloc = a + apple
        if s <= aloc and aloc <= t:
            yeiledApple += 1

    #orange
    for orange in oranges:
        oloc = b + orange
        if s <= oloc and oloc <= t:
            yeiledOrange += 1

    print(yeiledApple)
    print(yeiledOrange)

if __name__ == '__main__':
    st = input().split()
    s = int(st[0])
    t = int(st[1])
    ab = input().split()
    a = int(ab[0])
    b = int(ab[1])
    mn = input().split()
    m = int(mn[0])
    n = int(mn[1])
    apples = list(map(int, input().rstrip().split()))
    oranges = list(map(int, input().rstrip().split()))

    countApplesAndOranges(s, t, a, b, apples, oranges)

・denotes
~を表す
the red region denotes his house.

・メソッドの出力形式
print。メソッド実行で終了しているため。
countApplesAndOranges(s, t, a, b, apples, oranges)



問題は簡単だけど、設問が長い、、

0
2
4

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
2