0
点赞
收藏
分享

微信扫一扫

第九届蓝桥杯C++B组:螺旋折线


如图p1.png所示的螺旋折线经过平面上所有整点恰好一次。

第九届蓝桥杯C++B组:螺旋折线_折线

对于整点(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>

不能通过工程设置而省略常用头文件。

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

思路:找规律,固定四个象限角平分线(除第三象限)——都是矩形的四个顶点,把给定的坐标(x,y)和对应象限的顶点比较,以定点长度作为标准进行加减运算,最后得出长度。

参考代码:

#include <cstdio>
#include <algorithm>

using namespace std;

int ans;//长度
int main(){
int x,y;
scanf("%d%d", &x, &y);

if(x >= 0 && y >= 0){//第一象限
int temp = max(x,y);
int stand = 4 * temp * temp;//象限对角线上点的长度
if(x < y){//在对角线上点的左边
ans = stand - (y -x);
}else if(x == y){
ans = stand;
}else{//在对角线上点的右边
ans = stand + (x -y);
}
}else if(x >= 0 && y <= 0){//第四象限
int temp = max(x, abs(y));
int stand = 4 * temp * temp + 2 * temp;
if(x == -y){
ans = stand;
}else if(abs(y) < x){//在对角线上点的上边
ans = stand - (x - abs(y));
}else{//在对角线上点的左边
ans = stand + (abs(y) - x);
}
}else if(x <= 0 && y >= 0){//第二象限
int temp = max(abs(x), y);
int stand = 4 * temp * temp - 2 * temp;
if(-x == y){
ans = stand;
}else if(abs(x) < y){//在对角线上点的右边
ans = stand + (y - abs(x));
}else{//在对角线上点的下边
ans = stand - (abs(x) - y);
}
}else {//第三象限
int temp = min(x, y - 1);
int stand = temp + temp + 1;
stand = stand * stand;
if(x == y -1){
ans = stand;
}else if(x == temp){//在对角线上点的左上边
ans = stand + (abs(temp + 1) - abs(y));
}else{//在对角线上点的右边
ans = stand - (abs(temp) - abs(x));
}
}

printf("%d\n", ans);
return 0;
}



举报

相关推荐

0 条评论