uva 10161 - Ant on a Chessboard

我的思路是先找到所求数字n 所在第m圈对角线的元素为m^2-m+1,然后根据坐标差值求出n的坐标,注意奇偶坐标的处理。

 

 

/*
 * uva10161.cpp
 *
 *  Created on: 2013-4-16
 *      Author: kevinjiang
 */
#include<cstdio>
#include<cmath>

int main() {
//	setbuf(stdout,NULL);
	int n;
	while (scanf("%d", &n) != EOF) {
		if (n == 0)
		break;

		int m = (int) sqrt(n - 1) + 1;
		int diag = m * m - m + 1;
		int diff = n - diag;
		int x = m, y = m;

		if (diff >= 0)
		x = m - diff;
		else
		y = m + diff;

		if(m%2==1)
		printf("%d %d\n",x,y);

		else
		printf("%d %d\n",y,x);
	}

	return 0;
}

 

你可能感兴趣的:(uva 10161 - Ant on a Chessboard)