首先将 (x,y) 放到原点,(x1,y1) 放到 (x,y) 的右侧,然后判断两个点之间的关系,当两个点的距离 0 < diff <= 2*r 时,返回为1,否则就向左滚动一个圆。
#include <bits/stdc++.h>
using namespace std;
int diffCenCir(int x, int y, int x1, int y1) {
return sqrt(pow(x-x1, 2) + pow(y-y1, 2));
}
void transPos(int &x, int &y, int &x1, int &y1) {
// 将坐标转换到圆右边的坐标轴上
x1 -= x; y1-=y; x = 0; y = 0;
x1 = x + diffCenCir(x, y, x1, y1);
y1 = 0;
return;
}
int moveStep(int r, int x, int y, int x1, int y1) {
if (y!=0 || y1 != 0) transPos(x, y, x1, y1);
int diff = diffCenCir(x, y, x1, y1);
if (diff <= 2*r) return 1;
return moveStep(r, x, y, x1-2*r, y1) + 1;
}
int main() {
// 首先将坐标转换到坐标轴上
int r, x, y, x1, y1;
while (cin >> r && cin >> x && cin >> y && cin >> x1 && cin >> y1) {
cout << moveStep(r, x, y, x1, y1) << endl;
}
return 0;
}