zoukankan      html  css  js  c++  java
  • Codeforces Round #171 (Div. 2) A. Point on Spiral(模拟)

    A. Point on Spiral
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 test(s)
    Input
    0 0
    Output
    0
    Input
    1 0
    Output
    0
    Input
    0 1
    Output
    2
    Input
    -1 -1
    Output
    3
    这题做了好久……囧
    AC Code:
     1 #include <iostream>
     2 #include <string>
     3 #include <set>
     4 #include <map>
     5 #include <vector>
     6 #include <stack>
     7 #include <queue>
     8 #include <cmath>
     9 #include <cstdio>
    10 #include <cstring>
    11 #include <algorithm>
    12 using namespace std;
    13 #define LL long long
    14 #define cti const int
    15 #define ctll const long long
    16 #define dg(i) cout << "*" << i << endl;
    17 
    18 int main()
    19 {
    20     int x, y, ans;
    21     while(scanf("%d %d", &x, &y) != EOF)
    22     {
    23         if(abs(x) > abs(y))
    24         {
    25             if(x == 1) ans = 0;
    26             else if(x > 0) ans = (x - 1)* 4 + (x != 1 - y);
    27             else ans = (-x - 1) * 4 + 3;
    28         }
    29         else
    30         {
    31             if(!y) ans = 0;
    32             else if(y > 0) ans = (y - 1) * 4 + 2;
    33             else ans = (-y - 1) * 4 + 4;
    34             ans -= (x == y && x);
    35         }
    36         printf("%d\n", ans);
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    分布式锁的几种实现方式
    分布式锁简单入门以及三种实现方式介绍
    Redis 总结精讲
    Redis 总结精讲 看一篇成高手系统-4
    Request、Response 之 Http 请求
    定时任务 Cron表达式
    跑批利器--批处理应用程序
    使用MultiByteToWideChar转换UTF8为GBK(UTF8在Windows的代码页是CP_UTF8)
    了解 XML 数字签名
    QuickReport根据每行的内容长度动态调整DetailBand1的行高
  • 原文地址:https://www.cnblogs.com/cszlg/p/2952535.html
Copyright © 2011-2022 走看看