算数オリンピック

rittan-appleさんが参加した算数オリンピックに出題された問題をプログラミングを使って解いてみました。小学3年生でこんな難しい問題が出題されるとは!!

一直線にならぶ星

私達の住んでいる地球は太陽の周りを1年(365日)かけて1週しています。
あるところに太陽の周りを12日かけて1周するAの星と、20日かけて1周するBの星があります。
今、太陽とAの星とBの星が下の図のように一直線に並んでいます。
次に太陽とAの星とBの星が一直線に並ぶのは何日後ですか。
ただし、一直線に並ぶ順番と場所は問いません。

pos_x, pos_y = 0, 0  # 太陽の位置
r1 = 50              # 惑星1の軌道半径
r2 = 80              # 惑星2の軌道半径
p1 = 12              # 惑星1の公転周期(orbital period) 単位:日
p2 = 20              # 惑星2の公転周期
d = 0                # 日付

def setup():
    global pos_x, pos_y
    size(400, 400)
    textSize(18)
    pos_x = width / 2 + 50
    pos_y = height / 2 + 50

def draw():
    global d
    background(46, 42, 67)
    drawSun()
    drawPlanet1()
    drawPlanet2()
    drawGuide()

def keyPressed():
    global d
    if key == CODED:
        if keyCode == LEFT:
            d -= 0.1
        if keyCode == RIGHT:
            d += 0.1
            
def drawSun():
    noStroke()
    fill(209, 21, 43)
    ellipse(pos_x, pos_y, 20, 20)

def drawPlanet1():
    noFill()
    stroke(84, 80, 100)
    ellipse(pos_x, pos_y, r1 * 2, r1 * 2)
    noStroke()
    fill(194, 234, 211)
    x1 = r1 * cos(-d * (360.0 / p1) * (PI / 180))
    y1 = r1 * sin(-d * (360.0 / p1) * (PI / 180))
    ellipse(pos_x + x1, pos_y + y1, 10, 10)

def drawPlanet2():
    stroke(61, 58, 72)
    noFill()
    ellipse(pos_x, pos_y, r2 * 2, r2 * 2)
    noStroke()
    fill(226, 232, 82)
    x2 = r2 * cos(-d * (360.0 / p2) * (PI / 180))
    y2 = r2 * sin(-d * (360.0 / p2) * (PI / 180))
    ellipse(pos_x + x2, pos_y + y2, 10, 10)

def drawGuide():
    # 惑星直列補助線
    stroke(255)
    fill(0)
    if int(x1) == 0 and int(x2) == 0:
        line(pos_x, 0, pos_x, pos_y * 2)
        fill(255, 0, 0)

    if int(y1) == 0 and int(y2) == 0:
        line(0, pos_y, pos_x * 2, pos_y)
        fill(255, 0, 0)

    # 日付表示
    fill(255)
    text("day : " + str(d), 20, 40)

コメントする