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 ...
随机推荐
- javascript (js)中的基本概念
1. 基本数据类型 1.1 number (数字)在js中没有整形和浮点型的区分,所有的数字都是浮点型标识, 采用64位的浮点格式来表示数字.如果数字类型用在字符串连接表达式中,则会自动转换成字符串, ...
- Xml Schema的用途
Xml Schema的用途 1. 定义一个Xml文档中都有什么元素 2. 定义一个Xml文档中都会有什么属性 3. 定义某个节点的都有什么样的子节点,可以有多少个子节点,子节点出现的顺序 4. ...
- 从CMO到龙泉寺弟子:佛法改变人生
今天,移动精英开发社为大家请来了佛家弟子孫濟初师兄给大家分享学佛心得.濟初师兄根据自己的亲身经历,讲述了佛法对他人生的影响,以及他所获得的启示.本文系国内ITOM管理平台OneAPM授权转载. 孫濟初 ...
- 7 天玩转 ASP.NET MVC — 第 5 天
目录 第 1 天 第 2 天 第 3 天 第 4 天 第 5 天 第 6 天 第 7 天 0. 前言 欢迎来到第五天的学习.希望第一天到第四天的学习,你都是开心的. 1. Lab 22 - 增加 Fo ...
- POJ 2185 Milking Grid (KMP,求最小覆盖子矩阵,好题)
题意:给出一个大矩阵,求最小覆盖矩阵,大矩阵可由这个小矩阵拼成.(就如同拼磁砖,允许最后有残缺) 正确解法的参考链接:http://poj.org/showmessage?message_id=153 ...
- ***百度统计图表Echarts的php实现类,支持柱形图、线形图、饼形图
/** * 百度数据统计图表echart的PHP实现类 * * 原作者: * @author: chenliujin <liujin.chen@qq.com> * @since 2013- ...
- 从Config文件中读取节点的配置信息
下面是web.config中与本内容有关的细节 <appSettings> <add key="servername" value="www" ...
- Codeforces Round #335 (Div. 2) A. Magic Spheres 模拟
A. Magic Spheres Carl is a beginner magician. He has a blue, b violet and c orange magic spheres. ...
- CDOJ 1259 昊昊爱运动 II 线段树+bitset
昊昊爱运动 II 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r ...
- excel设置下拉菜单,并且不同值会显示不同颜色
工作中常常要用的excel,每次都会有新的需求,然后不会,然后百度,然后过段时间可能就又忘了,于是就想说,自己记录下来~~~因为自己用的都是2010,其实哪个版本都差不多,都是应该可以找到相应的按钮滴 ...