Problem:


宝探し II

蔵を片付けていたら、先祖の宝のありかへの行き方を記した古文書が見つかりました。この古文書にはおおよそ次のような事が書かれていました。

1. まず、町外れの井戸から、真東に 1m の地点に立ち、まっすぐ井戸の方向を向け。
2. 右回りに 90 度向きを変え、1m 直進したら、まっすぐ井戸の方向を向け。
3. 右回りに 90 度向きを変え、1m 直進したら、まっすぐ井戸の方向を向け。
4.                   〃
5.                   〃
6.                   ：

2 行目以降は、全く同じことが書かれていました。あなたは、宝を探そうと思いましたが、厄介なことに気がつきました。昔と違い建物が邪魔をしていて、まっすぐ井戸の方向をむこうにも井戸が見えなかったり 1m 直進しようにも直進できなかったりします。さらに、この古文書ときたら、1000 行近くあり、古文書どおりの作業をすることは、かなりの時間と体力が要ります。しかしながら、幸運なことに、あなたはコンピュータを利用することができます。

古文書に書かれている行数 n を入力して、宝のありかを出力するプログラムを作成してください。ただし、 n は 2 以上 1,000 以下の正の整数とします。

入力

複数のデータセットが与えられます。各データセットとして、古文書の行数を表す１つの整数 n が１行に与えられます。

入力は -1 で終わります。データセットの数は 50 を超えません。

出力

町外れの井戸から東に x (m)，北に y (m)の位置に宝があるとして、
各データセットごとに以下の形式で出力して下さい。

x
y

出力は実数とし、0.01 以下の誤差を含んでもよい。

Sample Input

3
6
-1

Output for the Sample Input

0.29 
1.71
-2.31 
0.80

参考

n =2, 3, 4, 5, 6 の宝の位置



Sample input:
3
6
-1


Sample output:
0.29
1.71
-2.31
0.80

Submission status: Wrong Answer

Solution code:
import math

k = 1.0/math.sqrt(2.0)
while True:
    n = int(input())
    if n == -1:
        break
    nx = 1.0
    ny = 0.0
    for i in range(n):
        dx = -ny
        dy = nx
        d = 1.0/math.sqrt(dx**2 + dy**2)
        nx = nx + d*dx
        ny = ny + d*dy
    print(nx)
    print(ny)

Submission status: Wrong Answer

Solution code:
import sys
for e in sys.stdin:
 if'-1'==e:break
 z=1
 for _ in[0]*~-int(e):d=z*1j;z+=d/abs(d)
 print(z.real)
 print(z.imag)

Submission status: Wrong Answer

Solution code:
A=[[1.0,0.0]]
N=1
def f(n):
    x,y=A[-1]
    for i in range(N,n):
        tmp=i**.5
        x = x-y/tmp
        y = y+x/tmp
        A.append([x,y])
    return len(A)

while 1:
    n = input()
    if n==-1:break
    elif n>N: N=f(n)
    a,b = A[n-1]
    print "%.2f\n%.2f" % (a,b)

Submission status: Wrong Answer

Solution code:
import math

while 1:
    n = input()
    if n == -1:
        break
    x, y = 0.0, 0.0
    radian = math.radians(-90)
    for i in xrange(n):
        radian += math.pi
        x += math.cos(radian)
        y += math.sin(radian)
        radian = math.atan2(y, x)
    print "%.2f\n%.2f" % (x, y)

Submission status: Accepted

Solution code:
import math
while True:
  n = int(input())
  if n == -1:
    break
  x, y = 1, 0
  for _ in range(n - 1):
    angle = math.atan2(y, x) + math.pi / 2
    x += math.cos(angle)
    y += math.sin(angle)
  print(x, y)

Submission status: Accepted

Solution code:
from math import atan2,radians,sqrt,sin,cos
import sys

t = 0
result = []
for i in range(1,1000):
    t += atan2(1, sqrt(i))
    result.append((cos(t)*sqrt(i+1), sin(t)*sqrt(i+1)))
    
for n in map(int, sys.stdin):
    if n == -1:
        break
    print(result[n-2][0])
    print(result[n-2][1])

Submission status: Accepted

Solution code:
from math import *
while True:
    n = input()
    if n == -1:
        break
    step = 1.0
    x,y,theta = step,0,pi
    for i in xrange(n-1):
        theta -= pi/2
        x += step*cos(theta)
        y += step*sin(theta)
        if x >= 0:
            theta = atan(y/x) - pi
        else:
            theta = atan(y/x)
    print round(x,2),round(y,2)

Submission status: Accepted

Solution code:
import sys
f = sys.stdin


while True:
    n = int(f.readline())
    if n == -1:
        break
    location = 1 + 0j
    for i in range(1, n):
        location += (location * 1j) / abs(location)
    print('{:.2f}'.format(location.real))
    print('{:.2f}'.format(location.imag))

Submission status: Accepted

Solution code:
import math

def doc_string():
    """
    I can fly.
    """
while True:
    s = int(raw_input())
    if s == -1:
        break
    x = 1.0
    y = 0.0
    dx = 0
    dy = 0
    for i in xrange(s - 1):
        dx = -y / math.hypot(x, y)
        dy = x / math.hypot(x, y)
        x += dx
        y += dy

    print x,y

Submission status: Accepted

Solution code:
import math
def f(p):
  k = math.atan2(p[1],p[0])
  return k+math.radians(180)
while True:
  n = int(input())
  if n == -1:
    break
  else:
    x = 1
    y = 0
    kakudo = 180
    n -= 1
    while n > 0:
      kakudo = f((x,y))
      kakudo -= math.radians(90)
      x += math.cos(kakudo)
      y += math.sin(kakudo)
      n -= 1
    print(x)
    print(y)

Submission status: Accepted

Solution code:
import math
while 1:
    n=int(raw_input())
    if n==-1:
        break
    r,theta=1.0,0.0
    for i in range(n-1):
        r=(r**2+1.0)**0.5
        theta+=math.asin(1.0/r)
    print r*math.cos(theta)
    print r*math.sin(theta)
exit()

Submission status: Accepted

Solution code:
import math
while 1:
    n=int(raw_input())
    if n==-1:
        break
    r=1.0
    th=0.0
    for i in xrange(n-1):
        r=(r**2+1)**(1.0/2)
        th+=math.asin(1/r)
    print("%.2f"%(r*math.cos(th)))
    print("%.2f"%(r*math.sin(th)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
while(1):
    n=int(input())
    if(n==-1): sys.exit()
    x=1.0
    y=0.0
    r=180.0
    for i in range(n-1):
        r-=90.0
        if r<180.0:
            r+=360.0
        y+= math.sin(r/180.0*math.acos(-1))
        x+= math.cos(r/180.0*math.acos(-1))
        r=math.atan2(y,x)*180.0/math.acos(-1)-180.0
        if r<180.0:
            r+=360.0
    print(x)
    print(y)

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if not n+1:
        break
    p = 1 + 0j
    for _ in range(n-1):
        d = p * 1j
        d /= abs(d)
        p += d
    print("{:.2f}\n{:.2f}".format(p.real, p.imag))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import math

for s in sys.stdin:
    n = int(s)
    if n == -1:
        break

    v = [1, 0]
    for i in range(n-1):
        w = [-v[1], v[0]]
        l = math.sqrt(w[0]**2 + w[1]**2)
        normalized = [w[0] / l, w[1] / l]

        vx = v[0] + normalized[0]
        vy = v[1] + normalized[1]
        v = [vx, vy]

    print(v[0])
    print(v[1])

Submission status: Accepted

Solution code:
import sys
f = sys.stdin

while True:
    n = int(f.readline())
    if n == -1:
        break
    location = 1 + 0j
    for i in range(1, n):
        location += (location * 1j) / abs(location)
    print('{:.2f}'.format(location.real))
    print('{:.2f}'.format(location.imag))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0076
"""
import sys
from math import atan, atan2, degrees, pi, cos, sin


Memo = []
def init_memo(n):
    global Memo
    Memo.append((0, 0))
    Memo.append((1, 0))
    for i in range(n):
        x = Memo[-1][0]
        y = Memo[-1][1]
        rad = atan2(-y, -x)
        rad -= pi / 2
        x += cos(rad)
        y += sin(rad)
        Memo.append((x, y))


def solve(n):
    global Memo
    return Memo[n]


def main(args):
    init_memo(1000)
    while True:
        n = int(input())
        if n == -1:
            break
        result = solve(n)
        print('{:.2f}'.format(result[0]))
        print('{:.2f}'.format(result[1]))

if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# Aizu Problem 0076: Treasure Hunt II
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


positions = {0: [0, 0], 1: [1, 1]}
direction = 135 * math.pi / 180
x, y = 1, 1
for step in range(2, 1001):
    x += math.cos(direction)
    y += math.sin(direction)
    positions[step] = [x, y]
    direction = math.atan2(x, -y)

while True:
    n = int(input())
    if n == -1:
        break
    print("%.2f" % positions[n-1][0])
    print("%.2f" % positions[n-1][1])

Submission status: Accepted

Solution code:
import math

k = 1.0/math.sqrt(2.0)
while True:
    n = int(input())
    if n == -1:
        break
    nx = 1.0
    ny = 0.0
    for i in range(n-1):
        dx = -ny
        dy = nx
        d = 1.0/math.sqrt(dx**2 + dy**2)
        nx = nx + d*dx
        ny = ny + d*dy
    print(nx)
    print(ny)

Submission status: Accepted

Solution code:
import math
n = 1001
x = [1 for i in range(n)]
y = [0 for i in range(n)]
ang = 0
for i in range(2, n):
    ang += math.asin(1/math.sqrt(i - 1))
    x[i] = x[i - 1] + math.cos(ang)
    y[i] = y[i - 1] + math.sin(ang)

while True:
    n = int(input())
    if n == -1:
        break
    print(x[n])
    print(y[n])

Submission status: Accepted

Solution code:
import sys
for e in sys.stdin:
 n=int(e)
 if n<0:break
 z=1
 for _ in range(n-1):
  d=z*1j
  d/=abs(d)
  z+=d
 print(f'{z.real}\n{z.imag}')

Submission status: Accepted

Solution code:
import sys
for e in sys.stdin:
 n=int(e)
 if n<0:break
 z=1
 for _ in range(n-1):
  d=z*1j
  d/=abs(d)
  z+=d
 print(z.real)
 print(z.imag)

Submission status: Accepted

Solution code:
import sys
for e in sys.stdin:
 z,n=1,int(e)
 if n<0:break
 for _ in range(n-1):
  d=z*1j;z+=d/abs(d)
 print(z.real)
 print(z.imag)

Submission status: Accepted

Solution code:
import sys
for e in sys.stdin:
 z,n=1,int(e)
 if n<0:break
 for _ in[0]*~-n:d=z*1j;z+=d/abs(d)
 print(z.real)
 print(z.imag)

Submission status: Accepted

Solution code:
import sys
for e in sys.stdin:
 if'-1\n'==e:break
 z=1
 for _ in[0]*~-int(e):d=z*1j;z+=d/abs(d)
 print(z.real)
 print(z.imag)

Submission status: Accepted

Solution code:
for e in iter(input,'-1'):
 z=1
 for _ in[0]*~-int(e):d=z*1j;z+=d/abs(d)
 print(z.real)
 print(z.imag)

Submission status: Accepted

Solution code:
for e in iter(input,'-1'):
 z=1
 for _ in[0]*~-int(e):d=z*1j;z+=d/abs(d)
 print(f'{z.real}\n{z.imag}')

Submission status: Accepted

Solution code:
# AOJ 0076 Treasure Hunt II
# Python3 2018.6.15 bal4u

import math

MAX = 1005
r, d = [0.0]*MAX, [0.0]*MAX
r[1] = 1.0
for i in range(2, MAX):
	d[i] = d[i-1] + math.atan(1 / r[i-1])
	r[i] = math.sqrt(r[i-1]**2 + 1)

while True:
	i = int(input())
	if i < 0: break
	print(r[i]*math.cos(d[i]), r[i]*math.sin(d[i]), sep='\n')

Submission status: Accepted

Solution code:
def f(x,y):
    d=(x*x+y*y)**0.5
    return -y/d,x/d
while True:
    n=input()
    if n==-1:
        break
    x,y=1.0,0
    for i in xrange(n-1):
        dx,dy=f(x,y)
        x,y=x+dx,y+dy
    print x,y

Submission status: Accepted

Solution code:
def f(x,y):
    d=(x*x+y*y)**0.5
    return x-y/d,y+x/d
while True:
    n=input()
    if n==-1:
        break
    x,y=1.0,0
    for i in xrange(n-1):
        x,y=f(x,y)
    print x,y

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin
from math import cos, sin, atan2, pi

PI2 = pi / 2
L = [None, (1.0, 0.0)]
for _ in range(2, 1001):
    x, y = L[-1]
    rad = atan2(y, x) + PI2
    L.append((x + cos(rad), y + sin(rad)))

for line in stdin:
    n = int(line)
    if n == -1:
        break
    print('{:0.2f}\n{:0.2f}'.format(*L[n]))

Submission status: Accepted

Solution code:
import math

while True:
    n = int(raw_input())
    if n == -1:
        break
    r, theta = 1.0, 0.0
    for i in range(n-1):
        r = math.sqrt(r**2 + 1.0)
        theta += math.asin(1.0/r)
    print r*math.cos(theta)
    print r*math.sin(theta)

Submission status: Accepted

Solution code:
A=[]
x=1.0
y=0.0
for i in range(1001):
    A.append([x,y])
    tmp=(x**2+y**2)**.5
    dx,dy=-y/tmp,x/tmp
    x+=dx
    y+=dy

while 1:
    n=input()
    if n==-1:break
    a,b=A[n-1]
    print "%.2f\n%.2f"%(a,b)

Submission status: Accepted

Solution code:
A=[]
x=1.0
y=0.0
for i in range(1,1001):
    A.append([x,y])
    tmp=i**.5
    x,y=x-y/tmp,y+x/tmp

while 1:
    n=input()
    if n==-1:break
    a,b=A[n-1]
    print "%.2f\n%.2f"%(a,b)

Submission status: Accepted

Solution code:
A=[]
x=1.0
y=0.0
for i in range(1,1001):
    A.append([x,y])
    tmp = i**.5
    x, y = x-y/tmp, y+x/tmp

while 1:
    n = input()
    if n==-1:break
    a,b = A[n-1]
    print "%.2f\n%.2f" % (a,b)

Submission status: Accepted

Solution code:
A=[[]]*1001
x=1.0
y=0.0
for i in range(1,1001):
    A[i]=[x,y]
    tmp=i**.5
    x,y=x-y/tmp,y+x/tmp
 
while 1:
    n = input()
    if n==-1:break
    print A[n][0]
    print A[n][1]

Submission status: Accepted

Solution code:
N=1001
A=[[]]*N
x=1.0
y=0.0
for i in range(1,N):
    A[i]=[x,y]
    tmp=i**.5
    x,y=x-y/tmp,y+x/tmp
 
while 1:
    n = input()
    if n==-1:break
    a,b=A[n]
    print a,b

Submission status: Accepted

Solution code:
import math

while 1:
    n = input()
    if n == -1:
        break
    x, y = 0.0, 0.0
    radian = math.radians(-90)
    for i in xrange(n):
        radian = (radian + math.pi / 2)
        x += math.cos(radian)
        y += math.sin(radian)
        radian = math.atan2(y, x)
    print "%.2f" % x
    print "%.2f" % y

Submission status: Accepted

Solution code:
import math

while 1:
    n = input()
    if n == -1:
        break
    x, y = 0.0, 0.0
    radian = math.radians(-90)
    for i in xrange(n):
        radian += math.pi / 2
        x += math.cos(radian)
        y += math.sin(radian)
        radian = math.atan2(y, x)
    print "%.2f\n%.2f" % (x, y)

Submission status: Accepted

Solution code:
def calc(k, ax, ay):
    bx = ax - ay/(k**0.5)
    by = ay + ax/(k**0.5)
    return(bx, by)


while True:
    n = int(input())
    if n == -1:
        break

    ax, ay = 1, 0
    for i in range(n-1):
        ax, ay = calc(i+1, ax, ay)
    
    print(round(ax, 3))
    print(round(ay, 3))

Submission status: Accepted

Solution code:
import math
while True :
    n = int(input())
    if n == -1 :
        break
    
    x = 1
    y = 0
    for i in range(n-1) :
        a = y / (math.sqrt(x**2 + y**2))
        b = x / (math.sqrt(x**2 + y**2))
        x = x - a
        y = y + b
    print('{:.2f}'.format(x))
    print('{:.2f}'.format(y))

Submission status: Accepted

Solution code:

while True:
    n = int(input())
    if n == -1:
        break
    x,y = 1,0
    for i in range(1,n):
        l = (x**2 + y**2)** -0.5
        x,y = x - l*y , y + l*x
    print(x)
    print(y)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
write = sys.stdout.write

L = 1000
D = [(0, 0), (1, 0)]
x = 1; y = 0
for i in range(L-1):
    d = (x**2 + y**2)**.5
    x, y = x - y/d, y + x/d
    D.append((x, y))

def solve():
    N = int(readline())
    if N == -1:
        return False
    x, y = D[N]
    write("%.16f\n%.16f\n" % (x, y))
    return True
while solve():
    ...

Submission status: Accepted

Solution code:

L = []
L.append((0,0))
L.append((1,0))
x = 1
y = 0
for _ in range(1000):
    dy = x /( (x**2 + y**2)**0.5 )
    dx = -y/( (x**2 + y**2)**0.5 )
    x += dx
    y += dy
    L.append((x,y))

while True:
    k = int(input())
    if k < 0:
        break
    print("{:.2f}".format(L[k][0]))
    print("{:.2f}".format(L[k][1]))

Submission status: Accepted

Solution code:
import math
from decimal import Decimal, ROUND_HALF_UP
def run(n):
    if len(pos)>=n:return(pos[n-1])
    else:run(n-1)
    x,y=pos[-1]
    d=math.degrees(math.atan(y/x))
    xi=x+math.cos(math.radians(d+[90,270][x<0]))
    yi=y+math.sin(math.radians(d+[90,270][x<0]))
    pos.append([xi,yi])
pos=[[1.0,0.0],[1.0,1.0]]
while 1:
    n=int(input())
    if n==-1:break
    run(n)
    print(Decimal(pos[n-1][0]).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))
    print(Decimal(pos[n-1][1]).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))

Submission status: Accepted

Solution code:
from math import pi,cos,sin,atan2

while(1):
    n = int(input())
    if n == -1:
        break
    n -= 1
    ang = 0.0
    x = 1.0
    y = 0.0
    for i in range(n):
        ang += pi/2
        x += cos(ang)
        y += sin(ang)
        ang = atan2(y,x)
    print("{:.2f}".format(x))
    print("{:.2f}".format(y))

Submission status: Runtime Error

Solution code:
# Aizu Problem 0076: Treasure Hunt II
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


positions = {0: [0, 0], 1: [1, 1]}
direction = 135 * math.pi / 180
x, y = 1, 1
for step in range(2, 10):
    x += math.cos(direction)
    y += math.sin(direction)
    positions[step] = [x, y]
    direction = math.atan2(x, -y)

while True:
    n = int(input())
    if n == -1:
        break
    print("%.2f" % positions[n-1][0])
    print("%.2f" % positions[n-1][1])

Submission status: Runtime Error

Solution code:
import math
n = 7
x = [1 for i in range(n)]
y = [0 for i in range(n)]
ang = 0
for i in range(2, n):
    ang += math.asin(1/math.sqrt(i - 1))
    x[i] = x[i - 1] + math.cos(ang)
    y[i] = y[i - 1] + math.sin(ang)

while True:
    n = int(input())
    if n == -1:
        break
    print(x[n])
    print(y[n])

Submission status: Runtime Error

Solution code:
import math

while True:
    n = int(raw_input())
    if n == -1:
        break
    r, theta = math.sqrt(2.0), pi/4.0
    for i in range(1,n-1):
        r = math.sqrt(r**2 + 1.0)
        theta += math.asin(1.0/r)
    print r*math.cos(theta)
    print r*math.sin(theta)

Submission status: Runtime Error

Solution code:
A=[]
x=1.0
y=0.0
for i in range(101):
    A.append([x,y])
    tmp=(x**2+y**2)**.5
    dx,dy=-y/tmp,x/tmp
    x+=dx
    y+=dy

while 1:
    n=input()
    if n==-1:break
    a,b=A[n-1]
    print "%.2f\n%.2f"%(a,b)