链接:点击打开链接
题意:给出一个三角形两个直角边长度a,b,问是否存在三个格点组成一个直角三角形使得直角边等于a,b,并且三边不平行于坐标轴
代码:
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
struct node{
int x,y;
}pos_a[1000005],pos_b[1000005];
int a,b;
int judge(int x,int y,int z){
return x*x+y*y==z*z;
}
int main(){ //只要以(0,0)为直角顶点再遍历即可
int i,j,k_a,k_b,sign;
while(scanf("%d%d",&a,&b)!=EOF){
k_a=k_b=sign=0;
for(i=1;i<=1000;i++)
for(j=1;j<=1000;j++)
if(judge(i,j,a))
pos_a[k_a].x=-i,pos_a[k_a++].y=j;
for(i=1;i<=1000;i++)
for(j=1;j<=1000;j++)
if(judge(i,j,b))
pos_b[k_b].x=i,pos_b[k_b++].y=j;
for(i=0;i<k_a;i++)
for(j=0;j<k_b;j++){
// cout<<pos_a[i].x<<" "<<pos_a[i].y<<endl;
// cout<<pos_b[j].x<<" "<<pos_b[j].y<<endl;
if(pos_a[i].y!=pos_b[j].y)
if((pos_b[j].x-pos_a[i].x)*(pos_b[j].x-pos_a[i].x)+(pos_b[j].y-pos_a[i].y)*(pos_b[j].y-pos_a[i].y)==a*a+b*b){
sign=1;
goto next;
}
}
puts("NO");
next:
if(sign){
puts("YES");
puts("0 0");
printf("%d %d\n",pos_a[i].x,pos_a[i].y);
printf("%d %d\n",pos_b[j].x,pos_b[j].y);
}
}
return 0;
}