1372: [蓝桥杯2018初赛]螺旋折线 【中 / 模拟】

在这里插入图片描述
http://oj.ecustacm.cn/problem.php?id=1372

模拟题:找规律。
在这里插入图片描述

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int x;
int y;
long long ans;
int main(void)
{
	cin>>x>>y;
    if(abs(x)<=y&&y>=0)//上
    {
        if(x>=0)
        ans=(long long)(2*y)*(2*y)-(y-x);//上右
        else
        ans=(long long)(2*y)*(2*y-1)+(y-abs(x));//上左
    }
    if(abs(y)<=x&&x>=0)//左
    {
        if(y>=0)
        ans=(long long)(2*x)*(2*x)+(x-y);
        else
        ans=(long long)(2*x)*(2*x+1)-(x-abs(y));
    }
    if(abs(x)<=abs(y)+1&&y<0)//下
    {
        if(x>=0)
        ans=(long long)(2*abs(y))*(2*abs(y)+1)+(abs(y)-x);
        else
        ans=(long long)(2*(abs(y)+1)-1)*(2*(abs(y)+1)-1)-(abs(y)+1-abs(x));
    }
    
    if(abs(y)<=abs(x)-1&&x<0)//右
    {
        if(y>=0)
        ans=(long long)(2*abs(x))*(2*abs(x)-1)-(abs(x)-y);
        else
        ans=(long long)(2*abs(x)-1)*(2*abs(x)-1)+(abs(x)-1-abs(y));
    }
    cout<<ans<<endl;
    return 0;
}

下面是y总写的,真的简洁,感觉自己写的好垃圾。

#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

int main()
{
    int x, y;
    cin >> x >> y;

    if (abs(x) <= y)  // 在上方
    {
        int n = y;
        cout << (LL)(2 * n - 1) * (2 * n) + x - (-n) << endl;
    }
    else if (abs(y) <= x)  // 在右方
    {
        int n = x;
        cout << (LL)(2 * n) * (2 * n) + n - y << endl;
    }
    else if (abs(x) <= abs(y) + 1 && y < 0)  // 在下方
    {
        int n = abs(y);
        cout << (LL)(2 * n) * (2 * n + 1) + n - x << endl;
    }
    else  // 在左方
    {
        int n = abs(x);
        cout << (LL)(2 * n - 1) * (2 * n - 1) + y - (-n + 1) << endl;
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值