题意:
有一块无限的格子板,每个单元占据1*1; 格子板上有一个坐标轴。 小明就住在这个格子板上面,每天早晨他从(x1,y1)出发,到达(x2,y2);
他通过的所有方格都被编号(边界和顶点不算); 你需要寻找一个小明经过的路径当中第n个方格的左下角的坐标。
分析:
就是一道暴力枚举题,不过精度很坑...不得已最后用了整数来避免精度误差...
有一块无限的格子板,每个单元占据1*1; 格子板上有一个坐标轴。 小明就住在这个格子板上面,每天早晨他从(x1,y1)出发,到达(x2,y2);
他通过的所有方格都被编号(边界和顶点不算); 你需要寻找一个小明经过的路径当中第n个方格的左下角的坐标。
分析:
就是一道暴力枚举题,不过精度很坑...不得已最后用了整数来避免精度误差...
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
long long x1, y1, x2, y2, n;
long long sum = 0, tx, ty;
scanf("%I64d%I64d%I64d%I64d%I64d", &x1, &y1, &x2, &y2, &n);
if(x1 == x2 || y1 == y2)
{
puts("no solution");
fclose(stdin);
fclose(stdout);
return 0;
}
tx = x2-x1, ty = y2-y1;
long long dx = tx>0?1:-1, dy = ty>0?1:-1;
tx = abs(tx), ty = abs(ty);
for(int x = 0; x < tx; ++x)
{
long long k1 = ty*x/tx, k2 = ty*(x+1)/tx;
if(ty*(x+1)%tx == 0)
sum += k2-k1;
else sum += k2-k1+1;
if(sum >= n)
{
int ans1 = x, ans2;
if(ty*(x+1)%tx == 0) ans2 = k2-(sum-n)-1;
else ans2 = k2-(sum-n);
if(dx == -1) ans1 = -ans1-1;
if(dy == -1) ans2 = -ans2-1;
printf("%I64d %I64d\n", ans1+x1, ans2+y1);
return 0;
}
}
puts("no solution");
return 0;
}