Hatena::Grouptopcoder

hama_DU@TopCoderへの道

2011-03-27SRM300台を練習していく part2

SRM 303 SpiralNumbers

|  SRM 303 SpiralNumbers - hama_DU@TopCoderへの道 を含むブックマーク はてなブックマーク -  SRM 303 SpiralNumbers - hama_DU@TopCoderへの道

http://www.topcoder.com/stat?c=problem_statement&pm=6093

やるだけ!


public class SpiralNumbers {

	public int[] dx = {-1, 0, 1, 0};
	public int[] dy = {0, 1, 0, -1};

	public String getPosition(int N) {
		long n = N;
		if (N == 1) {
			return "(" + 0 + "," + 0 + ")";
		}
		int a = 0;
		for (long i = 1 ; i <= 1000000 ; i += 2) {
			if (n <= i * i) {
				long proc = i * i - n;
				System.out.println(i);
				int sx = a;
				int sy = -a;
				for (int d = 0 ; d < 4 ; d++) {
					for (int b = 0 ; b < 2 * a ; b++) {
						if (proc == 0) {
							return "(" + sy + "," + sx + ")";
						}
						sx += dx[d];
						sy += dy[d];
						proc--;
					}
				}

				break;
			}
			a++;
		}
		return null;
	}

}