您的位置:首页 > 其它

Gym - 101177A Anticlockwise Motion 模拟

2017-11-05 00:06 399 查看
可以看成一个个嵌套的正方形

每个的左下角是 奇数 的平方,,然后模拟找到两个点的坐标

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define PI acos(-1.0)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1000 + 7, maxd = (1<<18)-1, mod = 1e9 + 7;
const int INF = 0x7f7f7f7f;
ll n, m;
struct node{
int x,y;
};
node pos_n,pos_m;
int judge_edge(int n){
int ans = (int)sqrt(n);
int cnt;
if(ans * ans != n){
if (ans % 2 ==0 ){
cnt =ans +1;
}
else {
cnt = ans +2;
}
}
else {
if (ans %2){
cnt = ans;
}
else{
cnt = ans +1 ;
}
}
return cnt;
}

void find_(int cen,int s){
int res = cen * cen;
if (s >= res - (cen - 1)){
pos_n.x = 1;
pos_n.y = 1 + (res-s);
}
else if (s <res-(cen-1) && s >= res - 2*(cen-1)){
pos_n.x = 1 + ((res - (cen-1) ) -s );
pos_n.y = (cen);
}
else if (s < res - 2* (cen - 1) && s >= res - 3* (cen -1)){
//cout <<"/*-*-/*-/*-/" <<endl;
int temp = res - 2* (cen-1);
pos_n.x = cen;
pos_n.y = cen - (temp - s );
}
else {
int temp = res - 3* (cen-1);
pos_n.y = 1;
pos_n.x = cen - (temp - s);
}
}

void find_1(int cen,int s){
int res = cen*cen;
if (s >= res - (cen - 1)){
pos_m.x = 1;
pos_m.y = 1 + (res-s);
}
else if (s < res-(cen-1) && s >= res - 2*(cen-1)){
pos_m.x = 1 + ((res - (cen-1) ) -s );
pos_m.y = (cen);
}
else if (s < res - 2* (cen - 1) && s >= res - 3* (cen -1)){
int temp = res - 2* (cen-1);
pos_m.x = cen;
pos_m.y = cen - (temp - s );
}
else{
int temp = res - 3* (cen-1);
pos_m.y = 1;
pos_m.x = cen - (temp - s);
}
}

int main() {
ios::sync_with_stdio(0);
cin >>n >>m;
if (n > m){
swap(n,m);
}
int cen_n = judge_edge(n);
int cen_m = judge_edge(m);
//cout << cen_n << " "<< cen_m<<endl;
int max_cen = max(cen_n,cen_m);
find_(cen_n,n);
find_1(cen_m,m);
//cout << pos_n.x <<" "<< pos_n.y <<" "<< pos_m.x << " " << pos_m.y << endl;
pos_n.x += (cen_m- cen_n)/2;
pos_n.y += (cen_m- cen_n)/2;
int ress = 0;
ress = abs(pos_m.x-pos_n.x) + abs(pos_m.y-pos_n.y);
cout << ress << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: