第九届蓝桥杯 螺旋曲线

标题:螺旋折线

如图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


资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

注意:
main函数需要返回0;
只使用ANSI C/ANSI C++ 标准;
不要调用依赖于编译环境或操作系统的特殊函数。
所有依赖的函数必须明确地在源文件中 #include <xxx>
不能通过工程设置而省略常用头文件。

提交程序时,注意选择所期望的语言类型和编译器类型。

 1 //这是一个规律题 
 2 #include <iostream>
 3 #include <cstring>
 4 #include <string>
 5 #include <cstdio>
 6 #include <queue>
 7 #include <map>
 8 #include <cmath> 
 9 #include <utility>
10 #include <vector>
11 #include <stack>
12 using namespace std;
13 #define ll long long
14 ll dis(ll y){
15      ll m=y;
16      y=abs(y);
17      ll ans;
18      if(m>=0){
19          ans = 4*y*y-y;
20      }
21      else{
22          ans = 4*y*y+3*y;
23      }
24      return ans;
25 }
26 ll x,y;
27 int main()
28 {
29     while(~scanf("%lld%lld",&x,&y)){
30         ll ans;
31         if(y>=0){
32         
33              if(abs(x)<=abs(y)){
34               ans = dis(y);
35                  ans+=x;
36              }
37              else{
38                  ans=dis(abs(x));
39                  if(x>0){
40                      ans+=(2*x-y);
41                  }
42                  else{
43                      ans+=(2*x+y);
44                  }
45              }
46         }
47         else{
48         
49             if(y-1<=x&&x<=-y){
50                  ans=dis(y);
51                 ans-=x;
52             }
53             else{
54                  ans=dis(y-1);
55                 if(x>0){
56                     ans-=(2*x+y);
57                 }
58                 else{
59                     ans+=(2*(-x)-1+y);
60                 }
61             }
62         }
63         printf("%lld
",ans);
64     }
65     return 0;
66 }
原文地址:https://www.cnblogs.com/tingtin/p/10567993.html