相似三角形
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Problem Description
给出两个三角形的三条边,判断是否相似。
Input
多组数据,给出6正个整数,a1,b1,c1,a2,b2,c2,分别代表两个三角形。(边长小于100且无序)
Output
如果相似输出YES,如果不相似输出NO,如果三边组不成三角形也输出NO。
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
本题中定义了一个三角形类,虽然是这么定义,但是由于输入数据之后就直接赋值了,所以增加了一个flag变量,来标记着究竟是不是一个三角形, 另外三边顺序不一定,所以在构造方法中直接排了下序方便操作,定义了一个判断两个三角形是否相似的方法用来判断两三角形是不是相似的。
package hello; import java.util.*; class Triangle{ int a, b, c; int flag; //如果flag为1 则这是一个三角形 public Triangle(int a[]) { Arrays.sort(a); //abc三条边依次增大 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"); } } } }