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

首先要判断给的数在第几层,比如说在第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. Masonry自动布局

    介绍,入门: http://www.cocoachina.com/ios/20141219/10702.html 下载: http://code.cocoachina.com/detail/30114 ...

  2. 在wpf窗体上添加用户控件

    1.引用用户控件的命名控件 xmlns:my="clr-namespace:WpfApplicationDemo.Control" 2.把用户控件添加到窗体中 <my:Use ...

  3. C++标准文档下载

    C++真正正式公布的标准只有三个:C++98.C++03.C++11. C++98是第一个正式的C++标准, C++03是在C++98上面进行了小幅度的修订, C++11则是一次全面的大进化(之前称C ...

  4. 检查和收集 Linux 硬件信息的 7 个命令

    http://blog.sae.sina.com.cn/archives/3910 在Linux系统中,有许多命令可用于查询主机的硬件信息.一些命令只针对特定的硬件组件,比如CPU.内存,一些命令可以 ...

  5. 学了C语言,如何利用cURL写一个程序验证某个网址的有效性?

    在<C程序设计伴侣>以及这几篇关于cURL的文章中,我们介绍了如何利用cURL写一个下载程序,从网络下载文件.可是当我们在用这个程序下载文件时,又遇到了新问题:如果这个网址是无效的,那么我 ...

  6. iOS在线音乐播放SZKAVPlayer(基于AVPlayer的封装)

    由于最近闲着没事,想找有关在线音乐播放的demo学习一下,在gitHub跟code4APP上面查找了很多帖子,结果很多在线音乐都是基于AudioStream实现的,我感觉用起来不太方便.后来突然发现, ...

  7. JTAG ARM-OB 被识别为盗版修复的方法

    今天下了一个 Keil 的最新版 V4.70,打开工程,弹出个升级Jlink固件的对话框,也没仔细看,直接点了yes .这下爽了,升级之后弹出个对话框说我的Jlink是盗版的,然后工程自动关闭,很是无 ...

  8. Minifilter微过滤框架:框架介绍以及驱动层和应用层的通讯

    minifilter是sfilter后微软推出的过滤驱动框架.相比于sfilter,他更容易使用,需要程序员做的编码更简洁. 系统为minifilter专门制作了一个过滤管理器,这个管理器本身其实是一 ...

  9. mysql模糊查询like/REGEXP

    原文:mysql模糊查询like/REGEXP 增删改查是mysql最基本的功能,而其中查是最频繁的操作,模糊查找是查询中非常常见的操作,于是模糊查找成了必修课. like模式 like意思是长得像, ...

  10. 怎样用delphi关闭并重新启动 explorer.exe进程

    uses Tlhelp32; function KillTask(ExeFileName:string):integer; const PROCESS_TERMINATE = $0001; var C ...