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的更多相关文章

  1. UVa 10161 Ant on a Chessboard

    一道数学水题,找找规律. 首先要判断给的数在第几层,比如说在第n层.然后判断(n * n - n + 1)(其坐标也就是(n,n)) 之间的关系. 还要注意n的奇偶.  Problem A.Ant o ...

  2. 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 ...

  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题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  5. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  6. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  7. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  8. Volume 1. Maths - Misc

    113 - Power of Cryptography import java.math.BigInteger; import java.util.Scanner; public class Main ...

  9. Jenkins 安装的HTML Publisher Plugin 插件无法展示ant生成的JunitReport报告

    最近在做基于jenkins ant  junit 的测试持续集成,单独ant junit生成的junitreport报告打开正常,使用Jenkins的HTML Publisher Plugin 插件无 ...

随机推荐

  1. C# ---- 串口数据YSI实例

    原文:C# ---- 串口数据YSI实例 C#----串口数据接收发送中,发送接收数据已经可以模拟了. 本次YSI实例过程中 主要是:类型转换问题 .计算校验码 一.不同设备不同的规则,本次实例代码如 ...

  2. php生成签名及验证签名

    <?php /** * 根据原文生成签名内容 * * @param string $data 原文内容 * * @return string * @author confu */ functio ...

  3. Android从无知到有知——NO.4

    因为我们做的是手机安全卫士,因此,我们需要一个地图定位功能,些相关的项目,也有一些教程.到百度官方下载了相关的jar包和API,但自己建项目的时候却不是那么顺利,bug不断,弄得心烦意乱,最后最终臣服 ...

  4. 我的Android 4 学习系列之文件、保存状态和首选项

    目录 使用Shared Preference 保留简单的应用程序数据 保存回话间的Activity实例数据 管理应用程序首选项和创建Preference Screen 保存并加载文件以及管理本地文件系 ...

  5. [Android] 获取WebView的页面标题(Title)-----WebChromeClient.onReceivedTitle()方法的重写

    应用开发中需要获取WebView当前页面的标题,可能通过对WebChromeClient.onReceivedTitle()方法的重写来实现 效果图如下: 代码如下: public class Mai ...

  6. asp.net mvc请求响应模型原理回顾

    根据讲师所讲总结了一下(可能存在些描述错误) -------------mvc进入asp.net管道原理: (在执行httpapplication管道之前mvc和asp.net是相同的,不同之处在于管 ...

  7. beanutils中WrapDynaBean

    public class Emp   { private String  firstName="李";    private String lastName;    public ...

  8. Java 感知Mysql存储过程变量数量

    在项目中,可能会遇到sybase 移植到 mysql的情况,因为sybase 支持存储过程的可变参数,而mysql不能支持,所以,在调用mysql的时候,需要感知存储过程到底有几个参数,来合理的配置参 ...

  9. ShopEx4.8.5.55328破解版

    ShopEx4.8.5.55328最新版完美破解版 http://club.1688.com/article/30369057.html?domainid=lhy16868 ShopEx4.8.5.5 ...

  10. Linq to sql 结

    ----左链接 var LeftJoin = from emp in ListOfEmployees join dept in ListOfDepartment on emp.DeptID equal ...