【MX-S8-T1】斐波那契螺旋 正解+解释
题目传送门
如题意描述,先把斐波那契数列(每个正方形的边长)处理出来,
然后就是单纯的把所有斐波那契数列的这个正方形搞出来就好,
我选择把每个正方形的左上右下全都确定,然后根据自己瞪眼找规律递推。
最后对于每个输出判断是否在正方形里面
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct q{
int x,xx,y,yy;
int v;
}s[100];
void init(){
s[1].v=1;
s[2].v=1;
for(int i=3;i<=94;i++){
s[i].v=s[i-1].v+s[i-2].v;
}
s[1].x=-1,s[1].xx=0,s[1].y=1,s[1].yy=0;
s[2].x=-1,s[2].xx=0,s[2].y=0,s[2].yy=-1;
for(int i=3;i<=94;i++){
//cnt%4==3 ???
if(i%4==3){
s[i].x=s[i-1].xx;
s[i].yy=s[i-1].yy;
s[i].xx=s[i].x+s[i].v;
s[i].y=s[i].yy+s[i].v;
}
//cnt%4==0 ???
if(i%4==0){
s[i].xx=s[i-1].xx;
s[i].yy=s[i-1].y;
s[i].x=s[i].xx-s[i].v;
s[i].y=s[i].yy+s[i].v;
}
//cnt%4==1 ???
if(i%4==1){
s[i].y=s[i-1].y;
s[i].xx=s[i-1].x;
s[i].yy=s[i].y-s[i].v;
s[i].x=s[i].xx-s[i].v;
}
//cnt%4==2 ???
if(i%4==2){
s[i].x=s[i-1].x;
s[i].y=s[i-1].yy;
s[i].xx=s[i].x+s[i].v;
s[i].yy=s[i].y-s[i].v;
}
}
}
int find(int x,int y){
for(int i=1;i<=94;i++){
if(x<=s[i].xx and x>=s[i].x and y>=s[i].yy and y<=s[i].y){
return s[i].v;
}
}
return -1;
}
main(void){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
init();
int T;
cin>>T;
while(T--){
int x,y;
cin>>x>>y;
cout<<find(x,y)<<endl;
}
}

浙公网安备 33010602011771号