第一条,时空双休(时间+空间)AC题解
2025-08-21 16:48:07
发布于:四川
2阅读
0回复
0点赞
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
int current = x; // 当前位置
int total_distance = 0; // 总距离
int step = 1; // 当前步长
int direction = 1; // 方向:1向右,-1向左
while (true) {
// 计算本次移动的终点
int next_pos = x + direction * step;
// 判断目标位置y是否在当前移动的路径上
if ((current < next_pos && current <= y && y <= next_pos) ||
(current > next_pos && next_pos <= y && y <= current)) {
// 如果在路径上,只需走到y即可
total_distance += abs(y - current);
break;
}
// 否则,走完整个步长
total_distance += abs(next_pos - current);
current = next_pos;
// 更新步长和方向,准备下一次移动
step *= 2;
direction *= -1;
}
cout << total_distance << endl;
return 0;
}
全部评论 2
1周前 来自 四川
1复制即可
1周前 来自 四川
1
有帮助,赞一个