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 }
  • 相关阅读:
    如何在当前文件夹打开cmd(基于win10)
    Python命名空间和作用域
    Python定位模块_PYTHONPATH变量
    Python模块_import语句_from...import 函数名_from ... import *
    Python全局变量和局部变量
    Python变量作用域
    Python匿名函数_return语句
    Python函数参数
    Python按值传递参数和按引用传递参数
    Python函数调用
  • 原文地址:https://www.cnblogs.com/cszlg/p/2952535.html
Copyright © 2011-2022 走看看