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 插件无 ...
随机推荐
- 2014Esri国际用户大会ArcGIS Online
1.基于什么是新的ArcGISOnline? ArcGISOnline不断更新.大约每四个月就会把新的增强的功能公布到各部分中.有新的空间分析的应用程序,如 Explorer forArcGIS,ap ...
- Android中利用Handler实现消息的分发机制(三)
在第二篇文章<Android中利用Handler实现消息的分发机制(一)>中,我们讲到主线程的Looper是Android系统在启动App的时候,已经帮我们创建好了,而假设在子线程中须要去 ...
- 查询DBlink创建
DBlink创建 查询 博客分类: Oracle 当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本地数据 ...
- ASP.NET 5 Identity
ASP.NET 5 Identity “跌倒了”指的是这一篇博文:爱与恨的抉择:ASP.NET 5+EntityFramework 7 如果想了解 ASP.NET Identity 的“历史”及“ ...
- 关于Installshield里一些常见问题的解答—艾泽拉斯之海洋女神出品
原文:关于Installshield里一些常见问题的解答-艾泽拉斯之海洋女神出品 上一篇:一个完整的安装程序实例—艾泽拉斯之海洋女神出品(五) --补遗转载时请务必保留转载出处和由艾泽拉斯之海洋女神出 ...
- 国内首篇介绍JanOS物联网操作系统的文章 - 如何把你的手机主板打造成物联网平台
天地会珠海分舵注:如无意外,您现在正在看的将是国内首篇且是唯一一篇介绍炙手可热的物联网的操作系统JanOS的文章!不信你去百度!希望大家能喜欢.但本文只是引言,更多信息请还是访问JanOS的官网:ht ...
- u-boot学习(两):u-boot简要分析
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 看到不错的文章.不要加入收藏夹, ...
- List<string>和string[]
List<string>和string[] List<string>是集合:string[]是数组: ///////////////////////////////////// ...
- [译]ava 设计模式之构造器
(文章翻译自Java Design Pattern: Builder) 构造器模式的关键之处在于它使用一步接招一步的流程去构建东西,例如:尽管构建的每一步是不相同的但是每一个产品还是遵循相同的流程. ...
- CF:Problem 427C - Checkposts良好的沟通 Tarjan算法
tarjan算法的第一个问题 喷我的脸....手写叠式开成BOOL,我一直在找错了... #include<cstdio> #include<cstring> #include ...