一道数学水题,找找规律。

首先要判断给的数在第几层,比如说在第n层。然后判断(n * n - n + 1)(其坐标也就是(n,n)) 之间的关系。

还要注意n的奇偶。

 Problem A.Ant on a Chessboard 

Background

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)

25

24

23

22

21

10

11

12

13

20

9

8

7

14

19

2

3

6

15

18

1

4

5

16

17

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

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.

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.

Sample Input

8

20

25

0

Sample Output

2 3

5 4

1 5

AC代码:

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; int main(void)
{
#ifdef LOCAL
freopen("10161in.txt", "r", stdin);
#endif int N;
while(scanf("%d", &N) == && N)
{
int n = (int)ceil(sqrt(N));
int x, y;
if(n & == )
{
if(N < n * n - n + )
{
x = n;
y = N - (n - ) * (n - );
}
else
{
y = n;
x = n * n - N + ;
}
}
else
{
if(N < n * n - n + )
{
y = n;
x = N - (n - ) * (n - );
}
else
{
x = n;
y = n * n - N + ;
}
} cout << x << " " << y << endl;
}
return ;
}

代码君

UVa 10161 Ant on a Chessboard的更多相关文章

  1. uva 10161 Ant on a Chessboard 蛇形矩阵 简单数学题

    题目给出如下表的一个矩阵: (红字表示行数或列数) 25 24 23 22 21 5 10 11 12 13 20 9 8 7 14 19 3 2 3 6 15 18 2 1 4 5 16 17 1 ...

  2. 10161 - Ant on a Chessboard

    Problem A.Ant on a Chessboard Background One day, an ant called Alice came to an M*M chessboard. She ...

  3. Uva10161 Ant on a Chessboard

    Uva10161 Ant on a Chessboard 10161 Ant on a Chessboard One day, an ant called Alice came to an M*M c ...

  4. UVA 12633 Super Rooks on Chessboard [fft 生成函数]

    Super Rooks on Chessboard UVA - 12633 题意: 超级车可以攻击行.列.主对角线3 个方向. R * C 的棋盘上有N 个超级车,问不被攻击的格子总数. 行列好好做啊 ...

  5. UVA 12633 Super Rooks on Chessboard(FFT)

    题意: 给你一个R*C的棋盘,棋盘上的棋子会攻击,一个棋子会覆盖它所在的行,它所在的列,和它所在的从左上到右下的对角线,那么问这个棋盘上没有被覆盖的棋盘格子数.数据范围R,C,N<=50000 ...

  6. UVA 12633 Super Rooks on Chessboard ——FFT

    发现对角线上的和是一个定值. 然后就不考虑斜着,可以处理出那些行和列是可以放置的. 然后FFT,统计出每一个可行的项的系数和就可以了. #include <map> #include &l ...

  7. [UVA 12633] Super Rooks on Chessboard FFT+计数

    如果只有行和列的覆盖,那么可以直接做,但现在有左上到右下的覆盖. 考虑对行和列的覆盖情况做一个卷积,然后就有了x+y的非覆盖格子数. 然后用骑士的左上到右下的覆盖特判掉那些x+y的格子就可以了. 注意 ...

  8. UVA 12633 Super Rooks on Chessboard (生成函数+FFT)

    题面传送门 题目大意:给你一张网格,上面有很多骑士,每个骑士能横着竖着斜着攻击一条直线上的格子,求没被攻击的格子的数量总和 好神奇的卷积 假设骑士不能斜着攻击 那么答案就是没被攻击的 行数*列数 接下 ...

  9. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

随机推荐

  1. IE浏览器上传文件时本地路径变成”C:\fakepath\”的问题【转】

    转自:http://www.iefans.net/ie-shangchuan-bendi-lujing-fakepath/ 在使用<input id="file_upl" t ...

  2. sqlite函数大全

      abs(X) 返回参数X的绝对值. coalesce(X,Y,...) 返回第一个非空参数的副本.若所有的参数均为NULL,返回NULL.至少2个参数. glob(X,Y) 用于实现SQLite的 ...

  3. POJ 2185 Milking Grid (KMP,求最小覆盖子矩阵,好题)

    题意:给出一个大矩阵,求最小覆盖矩阵,大矩阵可由这个小矩阵拼成.(就如同拼磁砖,允许最后有残缺) 正确解法的参考链接:http://poj.org/showmessage?message_id=153 ...

  4. iOS第三方支付-支付宝支付

    处理手机中有无支付宝的情况支付宝调用接口: - (void)payOrder:(NSString *)orderStr      fromScheme:(NSString *)schemeStr    ...

  5. DevExpress GridView 自定义搜索按钮改为中文内容

    首先将 GridControl 控件的搜索功能显示出来. http://www.cnblogs.com/DeepLearing/p/3887601.html 显示效果如下: 可以通过 GridLoca ...

  6. 传说中的WCF(6):数据协定(b)

    我们继续,上一回我们了解了数据协定的一部分内容,今天我们接着来做实验.好的,实验之前先说一句:实验有风险,写代码须谨慎. 实验开始!现在,我们定义两个带数据协定的类——Student和AddrInfo ...

  7. ios开发--企业帐号发布

    这两天需要发布一个ipa放到网上供其他人安装,需要用到企业级开发者账号. 首先详细说明一下我们的目标,我们需要发布一个ipa放到网上,所有人(包括越狱及非越狱设备)可以直接通过链接下载安装,不需要通过 ...

  8. 当前主流的安卓APP开发IDE

    什么是主流的开发安卓APP的方式? 我是去年4月份接触的Android开发,因此特别有感触,可以明显的感受到安卓APP主流开发方式的改变. 去年,2015年年初,各大安卓开发群大部分大牛在用Eclip ...

  9. Spring笔记——Spring+JDBC组合开发

      使用Spring+JDBC集成步骤如下:   1. 配置数据源 2. 配置事务.配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式 ...

  10. Java 虚拟机体系结构

    众所周知,Java源代码被编译器编译成class文件.而并不是底层操作系统可以直接执行的二进制指令(比如Windows OS的.exe文件).因此,我们需要有一种平台可以解释class文件并运行它.而 ...