没什么可说的 直接暴力n3,当然也可以排序完之后把它优化一下这样更好。
if前两项的和小于第三项就直接结束第二重循环。
import java.util.*; import java.math.*; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.PrintWriter; public class Main { public static void main(String args[])throws IOException { StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); in.nextToken(); int n = (int) in.nval; long num[] = new long[n]; for(int i=0;i<n;i++) { in.nextToken(); num[i] = (long) in.nval; } boolean dui = false; long sum = 0; Arrays.sort(num); yi:for(int i=0;i<n-2;i++) { er:for(int k=i+1;k<n-1;k++) { for(int j=k+1;j<n;j++) { if(num[i]+num[k]<num[j]) { break er; } if(check(num[i],num[k],num[j])==true) { out.print(num[i]+" "+num[k]+" "+num[j]); dui = true;; break yi; } } } } if(dui==false) out.print("No solution"); out.flush(); } public static boolean check(long x,long y,long z) { if(x+y>z&&x+z>y&&y+z>x) return true; else return false; } }