Triangle
Description
It is well known that three segments can make a triangle if and only if the sum of lengths of any two of them is strictly greater than the length of the third one. Professor Vasechkin has N segments. He asked you, if you could find at least one set of three segments among them which can be used by professor to make a triangle.
Input
The first line of the input contains the only integer number N (3≤ N≤ 1000). The following N lines contain the length of segments professor has. The length of any segment is the integer number from 1 to 10 500.
Output
Write to the output the length of segments requested by the professor — three numbers delimited by spaces. Write three zeros if there are no such three segments.
Sample Input
sample input |
sample output |
7 1 2 6 4 8 100 73 |
8 4 6 |
解题思路:
比较坑的位置就是:
public class Solution{
}AC代码:
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class Solution{
public static void main(String args[]){
Scanner sca = new Scanner(System.in);
while(sca.hasNext()){
int n = sca.nextInt();
BigInteger[] a = new BigInteger[n];
boolean flag = true;
for(int i = 0; i < n; i++)
a[i] = sca.nextBigInteger();
Arrays.sort(a);
for(int i = 0; i < n && flag; i++){
for(int j = i+1; j < n-1 && flag; j++){
if((a[i].add(a[j])).compareTo(a[j+1]) > 0){
flag = false;
System.out.println(a[i]+" "+a[j]+" "+a[j+1]);
}
}
}
if(flag)
System.out.println("0 0 0");
}
}
}
寻找可构成三角形的三边
本题要求从N个整数中找出三个数,这三数能作为三角形的三边长度。输入包括N个整数,输出则是这三个整数或0表示不存在这样的组合。采用排序和两层循环来解决此问题。
403

被折叠的 条评论
为什么被折叠?



