UVa 10161 Problem A.Ant on a Chessboard

/* coder: ACboy date: 2010-3-1 result: 1AC description: UVa 10161 Problem A.Ant on a Chessboard */ #include <cmath> #include <iostream> using namespace std; int main() { double n; #ifndef ONLINE_JUDGE freopen("10161.txt", "r", stdin); #endif while (cin >> n) { if (n == 0) break; int x, y; int k = (int)sqrt(n); int end = n - k * k; if (end == 0) { if (k % 2 == 1) { x = 1; y = k; } else { x = k; y = 1; } } else { int a, b; if (k % 2 == 1) { a = end / (k + 1); if (a == 0) { x = end; y = k + 1; } else { b = end - k - 1; x = k + 1; if (b == 0) y = k + 1; else { y = k - b + 1; } } } else { a = end / (k + 1); if (a == 0) { x = k + 1; y = end; } else { y = k + 1; x = 2 * k + 2 - end; } } } cout << x << " " << y << endl; } return 0; }

这题的关键在于寻找规律。

你可能感兴趣的:(UVa 10161 Problem A.Ant on a Chessboard)