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 }
  • 相关阅读:
    如何修改 gitlab 的项目名称
    Vue 项目中 webSocket 的使用(服务端是 Java Spring boot)
    如何能选到好的车牌号
    实现微信,浏览器,App中H5的路线规划
    H5 navigator.geolocation demo
    npm package.json 中版本指定符号: ~ 、^、*
    小程序中静态资源绝对路径的写法
    重置gvim8 ctrl+f的翻页功能
    [TS]Map的基本操作
    [TS]闭包测试
  • 原文地址:https://www.cnblogs.com/cszlg/p/2952535.html
Copyright © 2011-2022 走看看