similar triangles
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Problem Description
Given the three sides of two triangles, determine whether they are similar.
Input
Multiple sets of data, give 6 positive integers, a1, b1, c1, a2, b2, c2, representing two triangles respectively. (side length less than 100 and unordered )
Output
If it is similar, output YES, if it is not similar, output NO, and output NO if the three sides do not form a triangle.
Sample Input
1 2 3 2 4 6 3 4 5 6 8 10 3 4 5 7 8 10
Sample Output
NO YES NO
In this question, a triangle class is defined. Although it is so defined, since the data is directly assigned after the input data, a flag variable is added to mark whether it is a triangle or not. In addition, the order of the three sides is not necessarily, so the lower order is directly arranged in the construction method to facilitate the operation, and a method for judging whether two triangles are similar is defined to judge whether the two triangles are similar.
package hello; import java.util. *; class Triangle{ int a, b, c; int flag; //if flag is 1 then this is a triangle public Triangle(int a[]) { Arrays.sort(a); //The three sides of abc increase in turn this.a = a[0]; this.b = a[1]; this.c = a[2]; if(this.a+this.b>this.c&&this.a+this.c>this.b&&this.b+this.c>this.a) { flag = 1; } else { flag = 0; } } public boolean is_similar(Triangle t) { if((this.a*t.b==this.b*t.a) && (this.b*t.c==this.c*t.b) && (this.a*t.c==this.c*t.a)) return true; else return false; } } public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int a[] = new int[3]; int b[] = new int[3]; while(cin.hasNext()) { for(int i = 0; i < 3; i++) { a[i] = cin.nextInt(); } for(int i = 0; i < 3; i++) { b[i] = cin.nextInt(); } Triangle s = new Triangle(a); Triangle t = new Triangle(b); if(s.is_similar(t) && s.flag == 1&&t.flag == 1) { System.out.println("YES"); } else { System.out.println("NO"); } } } }