baru003のブログ

baruの雑記兼備忘録

AOJ 0016

問題リンク Treasure Hunt

・コメント
今回の問題は三角関数を使って解きました。
初めて三角関数をしようしてみたのですが、c/c++のようにmathをimportしなくても初めからjava.langに入っていることを知り少し感動しました(笑)

・ソース

import java.util.*;

public class P0016 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int forward, rotation;
		double x = 0, y = 0, angle = 0;
		while (true) {
			String[] str = sc.next().split(",");
			forward = Integer.parseInt(str[0]);
			rotation = Integer.parseInt(str[1]);
			// 終了判定
			if (forward == 0 && rotation == 0)
				break;
			// x,y座標インプット
			x += (double) forward * Math.sin(angle * (Math.PI / 180));
			y += (double) forward * Math.cos(angle * (Math.PI / 180));
			angle += (double) rotation;
		}
		// x,y座標表示
		System.out.println((int) x);
		System.out.println((int) y);
	}

}