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. IIS Web服务扩展中没有Asp.net 2.0

    服务器上安装了ASP.NET v2.0组件,但是在IIS的Web服务扩展中并没有找到ASP.NET v2.050727这项,这导致基于.NET2.0开发的网页都无法正常浏览. 解决方法:打开IIS,转 ...

  2. Solr搜索结果说明 (转)

    在admin页面,输入相关内容后,会返回xml格式的内容.说明如下: <?xml version="1.0" encoding="UTF-8"?> ...

  3. 浅谈提升C#正则表达式效率

     摘要:说到C#的Regex,谈到最多的应该就是RegexOptions.Compiled这个东西,传说中在匹配速度方面,RegexOptions.Compiled是可以提升匹配速度的,但在启动速度上 ...

  4. Android 第三方加固方案 对比 MD

    常见的第三方加固方案官网介绍 由于安卓APP是基于Java的,所以极容易被破解,一个不经过加固的APP犹如裸奔一样,毫无防备.之前曾有新闻报道,一些专职的APP打包黑产就是专门从各种渠道找到apk,通 ...

  5. impala-shell常用命令

    1.查看相关内网IP: cat /etc/hosts 2.进入impala: impala-shell; 3.显示数据库,数据表 show databases: show tables; 4.查看表结 ...

  6. RNN(Recurrent Neural Networks)公式推导和实现

    RNN(Recurrent Neural Networks)公式推导和实现 http://x-algo.cn/index.php/2016/04/25/rnn-recurrent-neural-net ...

  7. python性能还是不错的

    一个未优化的程序,跑了四天,字典长度有15万条,每条40个字段,跑得还算不错. 刚刚优化后的: 等运行两天后再看看效果...

  8. Docker-machine创建虚机时停在虚机启动的提示上,并且创建的虚机显示Ip Not found

    Docker-machine创建虚机时停在虚机启动的提示上,并且创建的虚机用docker-machine ls 列出来的时候显示Ip Not found, 是什么原因那? [答案] 看这个帖子: ht ...

  9. [Javascript] Check both prop exists and value is valid

    Sometime you need to check one prop exists on the object and value should not be ´null´ or ´undefine ...

  10. 【python】安装bcoding

    C:\Users\horn1>pip install bcodingCollecting bcoding Downloading https://files.pythonhosted.org/p ...