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 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;
}
时间: 2024-10-29 04:15:18

Uva10161 Ant on a Chessboard的相关文章

Ant on a Chessboard UVA 10161

说说:首先,最终蚂蚁的行走路线可以看成一直在沿着正方形的边沿走.而最大的正方形的边长是平方之后小于所给时间N的最大整数.这个最大的正方形的边长可以通过二分法获得.而且要注意的是根据边长的奇偶性,剩余路线的出发点可能在右下角,也可能在左上角.然后将剩余的要走的路再分成在到达顶点(即转折点)之前和之后,通过简单的数学计算最后就可以判断出蚂蚁最后的位置啦! 题目:  Ant on a Chessboard Background One day, an ant called Alice came to

UVa 10161 Ant on a Chessboard

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

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

计划,,留

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 一.<算法竞赛入门经典> 刘汝佳 (UVaOJ 351道题) 以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html "AOAPC I"

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

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO

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

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html “AOAPC I”是刘汝佳(大

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c

jmeter+ant+jenkins+mac报告优化

一.在上篇博客中生成的报告有两个问题: 1.date not defined 2.Min Time和Max Time显示成了NaN 二.Jmeter+Ant报告生成原理: 在解决问题之前,让我们先弄清楚Jmeter+Ant是生成报告的原理,知道原理后我们就可以很从容的解决问题了.另外,如果后续我们想定制报告也就很容易了. 1.在Jmeter的extras目录下,官方已经为我们提供了一个现成的实例,我们只需要在该目录下执行ant命令就可以生成一个数据文件Test.jtl和一个报告Test.html

Ant: Class not found: javac1.8

今天用ant,在选择build.xml,run as ant build后出错Ant: Class not found: javac1.8 分析问题:是否是eclipse中的ant版本和java的版本不匹配那? 解决方法如下: 1. 去下载一个最新版本的ant,解压到本地相应目录下,我下载的是apache-ant-1.9.7 2. 在eclipse中, windows-  references---Ant  ----run time,点击Ant home, 将路径制定到下载的ant目录下 3.