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;
}
Uva10161 Ant on a Chessboard的更多相关文章
- UVa 10161 Ant on a Chessboard
一道数学水题,找找规律. 首先要判断给的数在第几层,比如说在第n层.然后判断(n * n - n + 1)(其坐标也就是(n,n)) 之间的关系. 还要注意n的奇偶. Problem A.Ant o ...
- 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 ...
- 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 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- Volume 1. Maths - Misc
113 - Power of Cryptography import java.math.BigInteger; import java.util.Scanner; public class Main ...
- Jenkins 安装的HTML Publisher Plugin 插件无法展示ant生成的JunitReport报告
最近在做基于jenkins ant junit 的测试持续集成,单独ant junit生成的junitreport报告打开正常,使用Jenkins的HTML Publisher Plugin 插件无 ...
随机推荐
- [leetcode]Text Justification @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...
- 用LSTM生成武侠人名
http://magicly.me/2017/04/07/rnn-lstm-generate-name/?utm_source=tuicool&utm_medium=referral 之前翻译 ...
- hadoop2.2.0 centos 编译安装详解
http://blog.csdn.net/w13770269691/article/details/16883663 废话不讲,直切正题. 搭建环境:Centos x 6.4 64bit 1.安装JD ...
- 平均值(Mean)、方差(Variance)、标准差(Standard Deviation) (转)
http://blog.csdn.net/xidiancoder/article/details/71341345 平均值 平均值的概念很简单:所有数据之和除以数据点的个数,以此表示数据集的平均大小: ...
- (NGUI)UISprite 切换图集
UISprite是可以使用代码动态切换图集的 using UnityEngine; using System.Collections; public class SpriteAtlasTest : M ...
- Python编译exe
有几种办法,选择py2exe,从pip安装,还不行,下载看起来都比较老,还是在csdn上下载了一个64位版本for2.7的 http://download.csdn.net/download/henu ...
- java 上传图片压缩
public static void uploadFile(MultipartFile multfile, String filePath) throws Exception { File targe ...
- 判断一个整数是否是2的n次方
参考:http://bbs.csdn.net/topics/370058619 如题,如何判断一个整数是否是2的N次方,我能想到的方法有两个 1.一直除2,看最后是否等于1.(最笨的方法) 2.转换成 ...
- iOS DES ECB 模式加密
//iOS DES ECB 模式加密 #import <CommonCrypto/CommonCryptor.h> ,,,,,,,}; +(NSString *) encryptUseDE ...
- 微软BI 之SSAS 系列 - 实现Cube 以及角色扮演维度,度量值格式化和计算成员的创建
在熟悉完下面这三种维度的创建方式之后,就可以开始创建我们的第一个 Cube 了. SSAS 系列 - 自定义的日期维度设计 SSAS 系列 - 基于雪花模型的维度设计 SSAS系列 - 关于父子维度的 ...