Ant on a Chessboard

来源:互联网 发布:二手车 知乎 编辑:程序博客网 时间:2024/05/16 23:38

Ant on a Chessboard























SampleInput
8
20
25
0
SampleOutput
2 3
5 4
1 5








大意:

给出一组数据,数据表示小蚂蚁走到某个位置所花的时间,打印出这个位置的X,Y坐标

要点:

根据表格可知对角线的数据为i × i - 1,所以可与对角线数据进行对比得出坐标

代码:

#include <stdio.h>int main(){int n;while (scanf ("%d", &n) && n != 0){for (int i = 1; i <= n; i++){int num = i * i;if (num >= n){if (i % 2 == 0){int d = i * (i - 1) + 1;if (d < n) {printf ("%d %d\n", i, i - (n - d));}else if (d > n){printf ("%d %d\n", i - (d - n), i);}else printf ("%d %d\n", i, i);}else {int d = i * (i - 1) + 1;if (d < n){printf ("%d %d\n", i - (n - d), i);}else if (d > n){printf ("%d %d\n", i, i - (d - n));}else{printf ("%d %d\n", i, i);}}break;}}}return 0;}

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 rst 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 leftin a word, the path was like a snake.
For example, her rst 25 seconds went like this:
( the numbers in the grids stands for the time when she went into the grids)
25
24
23
22
21
5
10
11
12
13
20
4
9
8
7
14
19
3
2
3
6
15
18
2
1
4
5
16
17
1
1
2
3
4
5
At the 8-th second , she was at (2,3), and at 20-th 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
Input le will contain several lines, and each line contains a number
N
(1

N

2

10
9
), which stands
for the time. The le will be ended with a line that contains a number `
0
'.
Output
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.
0 0