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. OpenCV教程(45) harris角的检测(3)

          在前面一篇教程中,我们通过取局部最大值的方法来处理检测结果,但是从图像中可以看到harris角的分布并不均匀,在纹理颜色比较深的地方检测的harris角结果更密集一些.本章中,我们使用一个 ...

  2. maven项目里,junit的test程序不能访问src/test/resource下面的配置

    问题描述 最近在写单元测试,但是不想改动源代码,所以想自己在本test目录下建一个resouces文件夹并添加对应的配置文件,可是发现test程序无法读取这个resouces文件夹下的配置. 问题解决 ...

  3. pchar,pwidechar,pansichar作为返回参数时内存访问错误

    function Test:pachr: var   str: string; begin   str := 'Test Char';   result:=pchar(str); end; 上面的Te ...

  4. Sharepoint2013 Report Service初探

    首先需要建立相应的report报表 如图: 这里的sql如下: SELECT PC.Name AS Category, PS.Name AS Subcategory, DATEPART(yy, SOH ...

  5. Direct I/O,Synchronous I/O的概念

    Direct I/O概念: Direct I/O is a way to avoid entire caching layer in the kernel and send the I/O direc ...

  6. 跨平台APP----对Cordova,APPCan,DCloud,APICloud四大平台的分析

    前言: 移动开发是未来一个很重要的IT领域,而跨平台开发将是这一领域最重要的事情.         ----谷震平 一 兵器谱 在国外,最大的是Cordova(PhoneGap,2011年广泛流行), ...

  7. mysql启动报错cannot allocate memory for the buffer pool处理

    今天启动mysql服务器时失败了.去/var/log/mysql/查看error.log,报错信息如下: 160123 22:29:26 InnoDB: Initializing buffer poo ...

  8. 10款CSS3按钮 - 程序员再也不用为按钮设计而发愁了...

    这次主要给大家分享10款风格各异的CSS3按钮,如果你希望你的页面也能有很炫的样式,那么我相信这10款CSS3按钮就非常适合你,而且每一款都整理了源代码供参考,一起来看看吧. 1.绚丽的CSS3发光按 ...

  9. python对json的操作总结 zz

    Json简介:Json,全名 JavaScript Object Notation,是一种轻量级的数据交换格式.Json最广泛的应用是作为AJAX中web服务器和客户端的通讯的数据格式.现在也常用于h ...

  10. map练习

    /* 编写程序统计并输出所读入的单词出现的次数 */ /* //代码一:---用map索引实现惊人的简练 #include <iostream> #include <map> ...