英雄会(csdn pongo)题解之坐标和数字

来源:互联网 发布:女朋友爱发脾气 知乎 编辑:程序博客网 时间:2024/05/18 01:55

坐标和数字

题目详情:


如图所示,我们从0开始把整数写在两条直线上,0, 1, 2,  3分别写在(0,0), (1,1), (2,0)和(3, 1)这4点上,如图规律继续写下去。

现在给定坐标(x,y),问(x,y)位置的整数是多少,如果这个位置不存在整数,输入-1。其中, 0<=x,y<=1100。

-----------------------------------------------------------------------------------------------------------------------

这道题很简单,就是两条直线y=x和y=x-2。10分白得了,下面是C++代码

#include <stdio.h>#include <iostream>#include <string>using namespace std;class Test {public:    static int position (int   x,int   y){//if(y<=0) return -1;int res=-1;if(1>=x){if(x==y)res=x;}else{//x>=2int tmp=x-2;int div=tmp/2;int ys=tmp%2;if(x==y)res=(div+1)*4+ys;else if(y==x-2)res=(div+1)*4+ys-2;}        return res;    }};//start 提示:自动阅卷起始唯一标识,请勿删除或增加。int main(){   std::cout<<Test::position(5,6)<<std::endl;std::cout<<Test::position(5,3)<<std::endl;std::cout<<Test::position(7,7)<<std::endl;} //end //提示:自动阅卷结束唯一标识,请勿删除或增加。




0 0
原创粉丝点击