UVa 10161 Ant on a Chessboard
一道数学水题,找找规律。
首先要判断给的数在第几层,比如说在第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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- UVA 12633 Super Rooks on Chessboard [fft 生成函数]
Super Rooks on Chessboard UVA - 12633 题意: 超级车可以攻击行.列.主对角线3 个方向. R * C 的棋盘上有N 个超级车,问不被攻击的格子总数. 行列好好做啊 ...
- UVA 12633 Super Rooks on Chessboard(FFT)
题意: 给你一个R*C的棋盘,棋盘上的棋子会攻击,一个棋子会覆盖它所在的行,它所在的列,和它所在的从左上到右下的对角线,那么问这个棋盘上没有被覆盖的棋盘格子数.数据范围R,C,N<=50000 ...
- UVA 12633 Super Rooks on Chessboard ——FFT
发现对角线上的和是一个定值. 然后就不考虑斜着,可以处理出那些行和列是可以放置的. 然后FFT,统计出每一个可行的项的系数和就可以了. #include <map> #include &l ...
- [UVA 12633] Super Rooks on Chessboard FFT+计数
如果只有行和列的覆盖,那么可以直接做,但现在有左上到右下的覆盖. 考虑对行和列的覆盖情况做一个卷积,然后就有了x+y的非覆盖格子数. 然后用骑士的左上到右下的覆盖特判掉那些x+y的格子就可以了. 注意 ...
- UVA 12633 Super Rooks on Chessboard (生成函数+FFT)
题面传送门 题目大意:给你一张网格,上面有很多骑士,每个骑士能横着竖着斜着攻击一条直线上的格子,求没被攻击的格子的数量总和 好神奇的卷积 假设骑士不能斜着攻击 那么答案就是没被攻击的 行数*列数 接下 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
随机推荐
- 快速编写HTML,CSS代码的有力工具Emmet插件
Emmet 是一个编辑器插件,它以一种简写的语法规则可用于快速编写html或css文档内容,它支持多种编辑器. 从官网:http://emmet.io/ 可下载各个编辑器的插件.notepad++ 插 ...
- SPOJ375 Query on a tree(LCT边权)
之前做了两道点权的LCT,这次做一下边权的LCT.上网找了一下资料,发现对于边权的LCT有这么两种处理方法,一种是每条边建一个点,于是边权就转成点权了.另外一种则是每个边权对应到点权上,也就是每个点对 ...
- Raphaël 是一个小型的 JavaScript 库,用来简化在页面上显示向量图的工作。你可以用它在页面上绘制各种图表、并进行图片的剪切、旋转等操作。
点这里 在线效果演示:http://raphaeljs.com/pie.html http://raphaeljs.com github: https://github.com/DmitryBaran ...
- [读]剑指offer
研二的开始找工作了,首先祝愿他们都能够找到自己满意的工作.看着他们的身影,自问明年自己这个时候是否可以从容面对呢?心虚不已,赶紧从老严那儿讨来一本<剑指offer>.在此顺便将自己做题所想 ...
- JMeter性能测试介绍学习一
上一节中,我们了解了jmeter的一此主要元件,那么这些元件如何使用到性能测试中呢.这一节创建一个简单的测试计划来使用这些元件.该计划对应的测试需求. 1)测试目标网站是fnng.cnblogs.co ...
- FastJson与Gson小测试
最近用到Json来传输数据,找到两个比较简单的工具 Gson 和 FastJson随便测试一下两个工具的效率~ 1 package com.json.fast; import java.util.Ar ...
- ios开发中超简单抽屉效果(MMDrawerController)的实现
ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...
- javamail邮件中插入图片
转载 http://duanmumu.blog.163.com/blog/static/1911133502012715104016481/ // TODO Auto-generated method ...
- sdk manager更新超时 time out
sdk manager选择菜单tools->options,勾选下面的force https......重启sdk manager
- PHPStorm IDE 快捷键(MAC)
⌘——Command ⌃ ——Control ⌥——Option/Alt ⇧——Shift ⇪——Caps Lock fn——功能键就是fn 编辑 Command+alt+T 用 (if..else, ...