zoukankan      html  css  js  c++  java
  • uva 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 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

    这道题目开始没看懂,以为挺复杂,其实看懂了之后,发现也挺简单,没什么好总结的,那个表格给出的是时间,其实和题目中蚂蚁每走一步走多长没有什么关系,简化一下就是按照那个规律填表,给出一个数字,求这个数字的位置,注意是坐标,而不是第几行第几列,我就犯了这个错误,不过只要行列互换一下就可以了。

    用 sqrt(n) 求出这个数字在第几个周期,在根据奇偶处理就可以了。

     1 #include <iostream>
     2 #include <iomanip>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <algorithm>
     7 #include <cstdio>
     8 
     9 using namespace std;
    10 
    11 int main(void)
    12 {
    13     int n;
    14     while (cin >> n)
    15     {
    16         if(!n)    break;
    17         int k = floor(sqrt(n));
    18         if (k * k == n)    { if(k&1)cout<<"1 "<<k<<endl;else cout<<k<<" 1"<<endl; }
    19         else
    20         {
    21             int t = n - k * k;
    22             if (k&1)
    23             {
    24                 if (t <= k + 1)    cout<<n-k*k<<' '<<k+1<<endl;
    25                 else    cout<<' '<<k+1<<k+1-((n-k*k)%(k+1))<<endl;
    26             }
    27             else
    28             {
    29                 if (t<=k+1)    cout<<k+1<<' '<<n-k*k<<endl;
    30                 else    cout<<k+1-((n-k*k)%(k+1))<<' '<<k+1<<endl;
    31             }
    32         }
    33     }
    34 
    35     return 0;
    36 }
  • 相关阅读:
    [转]PostgreSQL 逻辑结构 和 权限体系 介绍
    [转]常见的动态规划问题分析与求解
    [转]如何在Windows 10中更改文件夹背景颜色
    []如何在Windows 10中更改文件夹背景颜色
    [转]Jackson 解析json数据之忽略解析字段注解@JsonIgnoreProperties
    [转]使用自定义HttpMessageConverter对返回内容进行加密
    php+redis实现消息队列
    利用redis List队列简单实现秒杀 PHP代码实现
    redis 缓存击穿 看一篇成高手系列3
    redis 延时任务 看一篇成高手系列2
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/2761098.html
Copyright © 2011-2022 走看看