2018年第九届蓝桥杯【C++省赛B组】【第七题:螺旋折线】

版权声明:若您发现我的博文中有纰漏或有其他想法,诚挚希望您能与我讨论。QQ1171297065 https://blog.csdn.net/qq_40758751/article/details/88763703

第七题
标题:螺旋折线
如图p1.png所示的螺旋折线经过平面上所有整点恰好一次。
对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度。
例如dis(0, 1)=3, dis(-2, -1)=9
给出整点坐标(X, Y),你能计算出dis(X, Y)吗?

å¨è¿éæå¥å¾çæè¿°

【输入格式】
X和Y  

对于40%的数据,-1000 <= X, Y <= 1000  
对于70%的数据,-100000 <= X, Y <= 100000  
对于100%的数据, -1000000000 <= X, Y <= 1000000000  

【输出格式】
输出dis(X, Y)  
【样例输入】
0 1

【样例输出】
3

分类讨论,不知道对不对啊

//#include<bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp make_pair
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100050;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;

struct node
{
    ll x, y;
};
int n;
ll x, y;
node a,b,c,d;

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while(cin >> x >> y)
    {
        ll ans = -1;

        if(x == 0 && y == 0){
            cout << 0 << endl;
            continue;
        }
        if(x < 0){
            a.x = x;
            a.y = x+1;
            int nl = abs(a.x) * 2 - 1;
            if(ans == -1 && y - a.y <= nl && y - a.y > 0){
                ans = 1 + (abs(a.y) + 1) * 8 * abs(a.y) / 2 + y - a.y;
            }
        }
        if(y <= 0){
            b.x = y-1;
            b.y = y;
            int nl = abs(b.x) * 2 - 1;
            if(ans == -1 && x - b.x <= nl){
                ans = 1 + (abs(b.y) + 1) * 8 * abs(b.y) / 2 - (x - b.x);
            }
        }
        if(x >= 0 ){
            c.x = x;
            c.y = x;
            int nl = c.x * 2;
            if(ans == -1 && c.y - y <= nl && c.y - y >= 0){
                ans = c.x * 4 * c.x + c.y - y;
            }
        }
        if(y > 0){
            d.x = y;
            d.y = y;
            int nl = d.x * 2;
            if(ans == -1 && d.x - x <= nl){
                ans = d.x * 4 * d.x - (d.x - x);
            }
        }
        cout << ans << endl;
    }

	return 0;
}
/*
7
-1 -1
0 2

*/

猜你喜欢

转载自blog.csdn.net/qq_40758751/article/details/88763703