sgu 299. Triangle 大數

299. Triangle

Time limit per test: 0.5 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



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 10500.

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.

Example(s)
sample input
sample output
7
1
2
6
4
8
100
73
8 4 6





import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main
{
    public static void main(String[] arg){
        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();

        BigInteger[] a = new BigInteger[1005];
        for(int i=0;i<n;i++) {
            a[i] = cin.nextBigInteger();
        }
        Arrays.sort(a,0,n);
        boolean f=true;
        for(int i=0;i+2<n;i++){
            if(a[i].add(a[i+1]).compareTo(a[i+2])==1){
                System.out.println(a[i]+" "+a[i+1]+" "+a[i+2]);
                f=false;
                break;
            }
        }
        if(f==true){
            System.out.println("0 0 0");
        }

    }




}


從大到小的

import java.lang.reflect.Array;  
import java.math.BigDecimal;  
import java.math.BigInteger;  
import java.util.*;  
  
public class Main {  
  
    public static void main(String[] args) {  
        Scanner cin=new Scanner(System.in);  
        BigInteger a[]={BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(7),   
                BigInteger.valueOf(2), BigInteger.valueOf(0)};  
        Comparator cmp = new MyComparator();  
        Arrays.sort(a, cmp);  
        System.out.println(Arrays.toString(a));  
    }  
    static class MyComparator implements Comparator<BigInteger>//從大到小排序  
    {  
        @Override  
        public int compare(BigInteger o1, BigInteger o2) {  
            if(o1.compareTo(o2)<=0)  
                return 1;  
            else if(o1.compareTo(o2)==0)  
                return 0;  
            else  
                return -1;  
        }  
          
    }  
}  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章