站长网 大数据 SGU 299 Triangle(大数)

SGU 299 Triangle(大数)

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,i

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

解题思路:

比较坑的位置就是:

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");
        }
    }  
}  

本文来自网络,不代表站长网立场,转载请注明出处:https://www.tzzz.com.cn/html/shuju/2021/0526/6651.html

作者: dawei

【声明】:站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。
联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部