uva 10161 - Ant on a Chessboard
November 8, 2012
Uva
One day, an ant called Alice came to an M*M chessboard. She wanted to go around all the grids. So she began to walk along the chessboard according to this way: (you can assume that her speed is one grid per second) At the first second, Alice was standing at (1,1). Firstly she went up for a grid, then a grid to the right, a grid downward. After that, she went a grid to the right, then two grids upward, and then two grids to the left…in a word, the path was like a snake. For example, her first 25 seconds went like this: ( the numbers in the grids stands for the time when she went into the grids) 5 4 3 2 1 1 2 3 4 5 At the 8th second , she was at (2,3), and at 20th second, she was at (5,4). Your task is to decide where she was at a given time. (you can assume that M is large enough) Input file will contain several lines, and each line contains a number N(1<=N<=2*10^9), which stands for the time. The file will be ended with a line that contains a number 0. For each input situation you should print a line with two numbers (x, y), the column and the row number, there must be only a space between them. 8 20 25 0 2 3 5 4 1 5 这道题目开始没看懂,以为挺复杂,其实看懂了之后,发现也挺简单,没什么好总结的,那个表格给出的是时间,其实和题目中蚂蚁每走一步走多长没有什么关系,简化一下就是按照那个规律填表,给出一个数字,求这个数字的位置,注意是坐标,而不是第几行第几列,我就犯了这个错误,不过只要行列互换一下就可以了。 用 sqrt(n) 求出这个数字在第几个周期,在根据奇偶处理就可以了。
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(void)
{
int n;
while (cin >> n)
{
if(!n) break;
int k = floor(sqrt(n));
if (k * k == n) { if(k&1)cout<<"1 "<<k<<endl;else cout<<k<<" 1"<<endl; }
else
{
int t = n - k * k;
if (k&1)
{
if (t <= k + 1) cout<<n-k*k<<' '<<k+1<<endl;
else cout<<' '<<k+1<<k+1-((n-k*k)%(k+1))<<endl;
}
else
{
if (t<=k+1) cout<<k+1<<' '<<n-k*k<<endl;
else cout<<k+1-((n-k*k)%(k+1))<<' '<<k+1<<endl;
}
}
}
return 0;
}