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 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
#include<stdio.h>
#include<math.h>
int main(void)
{
int n;
while(scanf("%d",&n)&&n)
{
int x=0,y=0;
int p=sqrt((double)n);
if(p*p==n&&p%2) {x=1;y=p;}
else if(p*p==n&&p%2==0){x=p;y=1;}
else if(p%2==0&&n!=p*p) {
if(n-p-1<=p*p){x=p+1;y=n-p*p;}
else{x=p-(n-1-p-p*p)+1;y=p+1;}
}
else if(p%2==1&&n!=p*p) {
if(n-p-1<=p*p){x=n-p*p;y=p+1;}
else{x=p+1;y=p-(n-1-p-p*p)+1;}
}
printf("%d %d\n",x,y);
}
return 0;
}
10161 - Ant on a Chessboard的更多相关文章
- UVa 10161 Ant on a Chessboard
一道数学水题,找找规律. 首先要判断给的数在第几层,比如说在第n层.然后判断(n * n - n + 1)(其坐标也就是(n,n)) 之间的关系. 还要注意n的奇偶. Problem A.Ant o ...
- 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 ...
- 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题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- Volume 1. Maths - Misc
113 - Power of Cryptography import java.math.BigInteger; import java.util.Scanner; public class Main ...
- Jenkins 安装的HTML Publisher Plugin 插件无法展示ant生成的JunitReport报告
最近在做基于jenkins ant junit 的测试持续集成,单独ant junit生成的junitreport报告打开正常,使用Jenkins的HTML Publisher Plugin 插件无 ...
随机推荐
- Asp.Net MVC5入门学习系列⑤
原文:Asp.Net MVC5入门学习系列⑤ 检查VS生产的编辑方法和编辑窗体 前面我们一步使用强类型,然后创建Controller(控制器)的时候,VS默认已经给我们把CURD都简单的实现了.这篇的 ...
- ATMEGA16 IOport相关汇总
ATMEGA16 IOport总的共同所有权有关的寄存器有三个DDRX.PORTX和PINX. 假设你想IO动作基本上与这三个寄存器操纵.DDRX也被称为数据方向寄存器.PORTX所谓数据寄存器.PI ...
- Android小应用-----画画板
public class MainActivity extends Activity { private ImageView iv; float startX = 0; float startY = ...
- SQLServer 2005 数据库定阅复制实现双机热备(主要是sharepoint 内容数据库)
原文:SQLServer 2005 数据库定阅复制实现双机热备(主要是sharepoint 内容数据库) 场景 公司最近的sharepoint的数据库服务器老是出问题,并且在一旦出现问题,就导致无法正 ...
- ANDROID定义自己的观点——模仿瀑布布局(源代码)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 在自己定义view的时候,事实上非常easy,仅仅须要知道3步骤: 1.測量- ...
- Scala Sublime text 3 Build 编译
使用Sublime text 3编译Scala 在sublime工具栏中[tools]->[Build System]->[new Build System]添加如下文本 { " ...
- 一个可以直接使用的MsgBox基于form居中API
可直接复制DialogBox项目(文件夹)到需要的项目中然后直接引用和using CodeProject.Dialog 已修正原作者代码错误的地方,可直接使用,VS2010测试成功 具体可以参考案例T ...
- android微信付费
原文地址:http://blog.csdn.net/intbird 微信官方文档地址:t=resource/res_main_tmpl&verify=1&lang=zh_CN" ...
- Talend open studio数据导入、导出、同步Mysql、oracle、sqlserver简单案例
推荐大家一个BI工具:talend open studio.我也是刚接触,懂得不多,感觉比较神奇就想大家推荐一下... 由于公司项目,接触了一下BI工具talend,感觉功能很强大, 可以同步多种数据 ...
- 有关Android存储的相关概念辨析
我想念许多Android开发人员在碰到有关存储的相关问题时,肯定遇到过“内部存储/内存”.“外部存储/外存”等类似的概念,尤其是将相关概念跟非开发人员解释时,那真是“秀才遇到兵,有理说不清哪”.包括我 ...