1. 程式人生 > >uva10161 Ant on a Chessboard

uva10161 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)

25

24

23

22

21

10

11

12

13

20

9

8

7

14

19

2

3

6

15

18

1

4

5

16

17

5

4

3

2

1

1          2          3           4           5

At the 8th second , she was at (2,3), and at 20th 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*10^9), 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

注:
本題目意思很好理解,就是以螺旋的形式數字從小到大,一圈一圈旋轉!輸入數字N,輸出N在螺旋圈中的座標!本題的關鍵就是找出規律,首先可以確定在一個正方形中找規律,經觀察發現,正方形中對角線上的元素可以表示為a*(a -1)+1,其中a為正方形的邊長!從而再確定所找的數位於邊長為奇數還是邊長為偶數的正方形的最外層邊界上!再更行判斷就可以得出結果了!!
以下是AC程式碼:
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int n , q ,ans;
    while(scanf("%d",&n) == 1)
    {
                if(!n)break;
                q = (int) ceil(sqrt(n));//找出正方形的邊長 
                ans = q * (q - 1) + 1;//對角線上的最外層的元素 
                if(q & 1)//注意判斷大小 
                {
                     if(n >= ans)
                          printf("%d %d\n",q -(n - ans),q);
                     else
                          printf("%d %d\n",q,q - (ans - n));
                }
                else
                {
                    if(n >= ans)
                        printf("%d %d\n",q,q -(n - ans));
                    else
                        printf("%d %d\n",q -(ans - n),q);
                        
                }
    }
    return 0;
}
                                   
    


相關推薦

Uva10161 Ant on a Chessboard

function pen spl like auth add several script was Uva10161 Ant on a Chessboard 10161 Ant on a Chessboard One day, an ant call

uva10161 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 t

Tomcat:Can't load IA 32-bit .dll on a AMD 64-bit platform問題的解決

myeclips iges create version mage dlink conf tomcat服務器 apach 控制臺錯誤如下: java.lang.UnsatisfiedLinkError: D:\apache-tomcat-7.0.56\bin\tcnat

[ACM] POJ 1942 Paths on a Grid (組合)

遞歸 clu 位數 pict 數位 end lesson weight 運算 Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21297 Ac

Spoj 10628. Count on a tree

open class style pen 技術 += 初始 medium poj Description 給定一棵N個節點的樹,每個點有一個權值,對於M個詢問(u,v,k),你需要回答u xor lastans和v這兩個節點間第K小的點權。其中lastans是上一個詢

How to center body on a page?

however round ppr one man mil ati roman time ?【提問】 I‘m trying to center the body element on my HTML page. Basically, in the CSS I

SPOJ QTREE - Query on a tree

deep c++ ble else pac wap name cas for each QTREE - Query on a tree #tree You are given a tree (an acyclic undirected connecte

leetcode max-points-on-a-line

light .get 垂直 題目 keys plan etc highlight subject 題目描述 Given n points on a 2D plane, find the maximum number of points that lie on the

1097. Deduplication on a Linked List (25)

ica mes clas struct nod main style abs system Linkedlist的題目,多給一個index,然後根據要求排序,經常會有奇效。。。大神的思路果然厲害。。。 #include<iostream> #include&l

2017廣西邀請賽 Query on A Tree (可持續化字典樹)

題意 second for each follow n) nod pair content back Query on A Tree 時間限制: 8 Sec 內存限制: 512 MB提交: 15 解決: 3[提交][狀態][討論版] 題目描述 Monkey

hdu 6191 Query on A Tree(dfs序+可持久化字典樹)

none turn const blog 節點 set for clear amp 題目鏈接:hdu 6191 Query on A Tree 題意: 給你一棵樹,每個節點有一個值,現在有q個詢問,每個詢問 詢問一個u x,問以u為根的子樹中,找一個節點,使得這個節點的值與

Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted

perm eve mit can source string per ted idt 在java中寫switch代碼時,參數用的是string,jdk用的是1.8,但是還是報錯,說不支持1.7版本以下的,然後查找了項目中的一些文件,打開一個文件如下,發現是1.6的版本,好奇

【maven】maven的web項目打包報錯:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK

應用 cga snapshot ace owin span ons sed sse 打包過程中報錯如下: No compiler is provided in this environment. Perhaps you are running on a JRE rather

poj 2104 C - Count on a tree

none oid log uniq sin aps urn sca amp #include<iostream> #include<cstdio> #include<algorithm> #include<set> #in

hdu 6191--Query on A Tree(持久化字典樹)

out trie scribe nodes include mathjax osi lan push_back 題目鏈接 Problem Description Monkey A lives on a tree, he always plays on this t

149. Max Points on a Line

pri gcd con class ret solution i+1 return [] class Solution { public int maxPoints(Point[] points) { if(points.length<

leetcode149- Max Points on a Line- hard

hat pla 而不是 mat 起點 ash esc ont 情況 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

Count on a tree II

limit 時間 序號 time 就是 出現一次 ria sse 處理 You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer

Max Points on a line

相同 map nta ber 通過 n-1 新的 def math Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. B

Query on a tree I

contain from c++ and build pre pos blank inpu You are given a tree (an acyclic undirected connected graph) with N nodes, and edges number