Symmetry

mac2022-06-30  23

【题目描述】 The figure shown on the left is left-right symmetric as it is possible to fold the sheet of paper along a vertical line, drawn as a dashed line, and to cut the figure into two identical halves. The figure on the right is not left-right symmetric as it is impossible to find such a vertical line. Write a program that determines whether a figure, drawn with dots, is left-right symmetric or not. The dots are all distinct. 【输入格式】 The input consists of T test cases. The number of test cases T is given in the first line of the input file. The first line of each test case contains an integer N , where N ( 1N1, 000) is the number of dots in a figure. Each of the following N lines contains the x-coordinate and y-coordinate of a dot. Both x-coordinates and y-coordinates are integers between -10,000 and 10,000, both inclusive. 【输出格式】 Print exactly one line for each test case. The line should contain ‘YES’ if the figure is left-right symmetric. and `NO’, otherwise. The following shows sample input and output for three test cases. 【样例输入】 3 5 -2 5 0 0 6 5 4 0 2 3 4 2 3 0 4 4 0 0 0 4 5 14 6 10 5 10 6 14 【样例输出】 YES NO YES 【分析】 容易想到,如果存在对称轴,那么对称轴的横坐标一定是最左边的点和最右边的点的中点。为了避免中点是小数 可以将横坐标都乘上2,然后在判断所有点是否有对称点就行了。

var x,y:array[0..1001]of longint; left,right,i,n,mid,t:longint; flag:boolean; function check(k:longint):boolean; var i:longint; begin for i:=1 to n do if (y[i]=y[k])and(x[i]+x[k]=2*mid) then exit(true); exit(false); end; begin readln(t); while t>0 do begin dec(t); left:=1;right:=1; readln(n); for i:=1 to n do begin readln(x[i],y[i]); x[i]:=x[i]*2; if x[i]<x[left] then left:=i; if x[i]>x[right] then right:=i; end; mid:=(x[left]+x[right]) div 2; flag:=true; for i:=1 to n do if not check(i) then flag:=false; if flag then writeln('YES') else writeln('NO'); end; end.

转载于:https://www.cnblogs.com/JRX2015U43/p/6533531.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)