Uva10161 Ant on a Chessboard

10161 Ant on a Chessboard

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)

见下图

At the 8-th second , she was at (2,3), and at 20-th 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∗109), 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



找出对角线的规律后,进行计算

/*
Author:ZCC;
Time:2015-6-4
Solve:对角线成差成等差数列。所以能够算出通项公式A_n=n*n-n+1
*/ #include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
const int maxn=44725;
typedef long long LL; LL a[maxn]; int main(){
//freopen("Text//in.txt","r",stdin);
for(int i=0;i<maxn;i++){
a[i]=(LL)i*i-i+1;
// if(a[i]>2000000000){cout<<i<<endl;break;}
}
LL n;
while(scanf("%lld",&n)&&n){
int pos=lower_bound(a+1,a+maxn,n)-a;
// cout<<"**"<<pos<<"**"<<endl;
int x=pos,y=pos;
if(pos&1){
if(n>=a[pos]-pos+1){
while(n<a[pos])n++,y--;
}
else {
x--;
y=pos-1;
pos--;
while(n>a[pos])n--,y--;
}
}
else {
if(n>=a[pos]-pos+1){
while(n<a[pos])n++,x--;
}
else {
y--;
x=pos-1;
pos--;
while(n>a[pos])n--,x--;
}
}
printf("%d %d\n",x,y);
}
return 0;
}

Uva10161 Ant on a Chessboard的更多相关文章

  1. UVa 10161 Ant on a Chessboard

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

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

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

  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. Spring(AbstractRoutingDataSource)实现动态数据源切换

    转自: http://blog.51cto.com/linhongyu/1615895 一.前言 近期一项目A需实现数据同步到另一项目B数据库中,在不改变B项目的情况下,只好选择项目A中切换数据源,直 ...

  2. VS2010新建Web网站与新建Web应用程序的区别 (转)

    在Visual Studio 2010中,除了可以使用“创建Web应用程序”的方式来构建自己的Web项目之外,还可以通过创建“Web网站”的方式来构建Web项其中,Web网站的创建方法:打开Visua ...

  3. GIST特征描述符使用

    来源:http://www.cnblogs.com/justany/archive/2012/12/06/2804211.html 一种场景特征描述 场景特征描述? 通常的特征描述符都是对图片的局部特 ...

  4. Convert Sorted List to Binary Search Tree leetcode java

    题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...

  5. Backbone.js 中使用 Model

    前面几篇 Backbone.js 的例子中有使用到 template, 及数据的填充,其实这已经很接近 Model 了.现在来学习怎么创建自己的 Model 类,并简单的使用.Backbone.js ...

  6. IIS配置中出现HRESULT:0X80070020错误

    Win7 IIS启动失败.手工启动它,提示:“另一个程序正在使用此文件,进程无法访问!” 此时是因为另一个程序占用了IIS的端口号,IIS一般用的是80端口,是谁占用了这个端口呢? 方法如下:开始菜单 ...

  7. OpenNebula学习第一节OpenNebula Front-end Installation

    一.说说情怀 随着公司硬件开发资源的不足,构建一个云平台似乎重要了起来.当然,也不是这个平台搭建的主力,出于工作的需求和个人兴趣爱好,接下来就来学习一下OpenNebula相关的东西,这是第一节课,先 ...

  8. (转)径向模糊效果shader

    转自:http://blog.csdn.net/xoyojank/article/details/5146297 最先在这里看到:http://www.gamerendering.com/2008/1 ...

  9. Sqlite的多表连接更新

    最近处理一个较大数据的sqlite库,基础表300万条,结果表30万条左右,我的笔记本跑起来还算流畅.最后结果,需要两个表连接,把另一个表的计算结果更新过来,却遇到麻烦.sqliter并不支持常见的连 ...

  10. nod32的内网在线更新设置

    需要一个iis地址,最好能够目录浏览,权限够大. 还必须: 增加一个MIME类型,.ver,类型填写application/x-ver