起因
周测一题
学姐起始坐标(0,0)。每次只能从(x1,y1)移动至(x2,y2),
并且两点距离为整数.
求从(0,0)移动至(x,y)的最小步数.
第一行为单独整数t(1<=t<=3000),代表样例个数。
每个样例包含x和y,代表目的地坐标
样例
1
3 4
输出
1
C++ 参考代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;
int main()
{
int t;
cin >> t;
int a,b;
while(t--)
{
cin >> a >> b;
int k=a*a+b*b;
if(a+b)
{
if(sqrt(k)==floor(sqrt(k)))
{
cout << "1" << endl;
}else
{
cout << "2" << endl;
}
}else
{
cout << "0" << endl;
}
}
return 0;
}
问题–如何判断两点间距离是否为整数
1.可以使用floor函数,自动向下取整.
k=x*x+y*y
sqrt(k)==floor(sqrt(k))
2.可以巧用强制类型转换.
double z=sqrt(x*x+y*y);
int k = (int)z;
if(k == z)
.....
```