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 }
  • 相关阅读:
    Java的字符串及格式化输入输出
    Java的数据类型与类型转换
    java基本程序
    svn基础入门
    github基础入门笔记
    git基础入门笔记
    linux基础入门笔记
    二、FreeMarker 模版开发指南 第二章 数值和类型
    【CodeForces】[599B]Spongebob and Joke
    【CodeForces】[612B]HDD is Outdated Technology
  • 原文地址:https://www.cnblogs.com/cszlg/p/2952535.html
Copyright © 2011-2022 走看看