zoukankan      html  css  js  c++  java
  • C

     
    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)][(1, 0), (1, 1)][(1, 1), ( - 1, 1)][( - 1, 1), ( - 1,  - 1)][( - 1,  - 1), (2,  - 1)],[(2,  - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.

    Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).

    Input

    The first line contains two space-separated integers x and y(|x|, |y| ≤ 100).

    Output

    Print a single integer, showing how many times Valera has to turn.

    Sample Input

    Input
    0 0
    Output
    0
    Input
    1 0
    Output
    0
    Input
    0 1
    Output
    2
    Input
    -1 -1
    Output
    3

    题意:给出一个从原点开始螺旋前进的移动规则,现要到达有限范围内的某个坐标,问一共需要几次转向。
    模拟。
    思路:画一下图找下规律就OK了,开始我以为输入的点只是该点所在正方形上的顶点,其实它是该正方形上任意一个整数点。
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     int x,y;
    10     while(~scanf("%d %d",&x,&y))
    11     {
    12         if(x == 0 && y == 0)
    13         {
    14             printf("0
    ");
    15             continue;
    16         }
    17         int k = max(abs(x),abs(y));
    18         int ans = (k-1)*4;
    19         if(x >= k-1 && x <= k && y == 1-k)
    20             printf("%d
    ",ans);
    21         else if(x == k && y >= 1-k && y <= k)
    22             printf("%d
    ",ans+1);
    23         else if(x >= -k && x <= k && y == k)
    24             printf("%d
    ",ans+2);
    25         else if(x == -k && y >= -k && y <= k)
    26             printf("%d
    ",ans+3);
    27         else if(x >= -k && x <= k && y == -k)
    28             printf("%d
    ",ans+4);
    29     }
    30     return 0;
    31 }
    View Code
  • 相关阅读:
    leetcode_1423. 可获得的最大点数
    leetcode_剑指 Offer 06. 从尾到头打印链表
    leetcode_剑指 Offer 05. 替换空格
    leetcode_49. 字母异位词分组
    leetcode_73. 矩阵置零
    leetcode_26. 删除排序数组中的重复项
    jstack查看JVM堆栈信息
    如何画一张架构图
    百年孤独家谱
    阿尔萨斯(Arthas)入门
  • 原文地址:https://www.cnblogs.com/LK1994/p/3443732.html
Copyright © 2011-2022 走看看