Advertisement
YavorGrancharov

Three_Integers_Sum

Dec 2nd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. mport java.util.Scanner;
  2.  
  3. public class Three_Integers_Sum {
  4.     public static void main(String[] args) {
  5.         Scanner console = new Scanner(System.in);
  6.  
  7.         String line = console.nextLine();
  8.         String[] arr = line.split("\\s+");
  9.         int[] nums = new int[arr.length];
  10.  
  11.         for (int i = 0; i < arr.length; i++) {
  12.             nums[i] = Integer.parseInt(arr[i]);
  13.         }
  14.  
  15.         for (int i = 0; i < nums.length; i++) {
  16.             for (int j = 0; j < nums.length; j++) {
  17.                 if (nums[i] < nums[j]) {
  18.                     int temp = nums[i];
  19.                     nums[i] = nums[j];
  20.                     nums[j] = temp;
  21.                 }
  22.             }
  23.         }
  24.  
  25.         int sum;
  26.         boolean contains = false;
  27.         for (int i = 0; i < nums.length; i++) {
  28.             int first = nums[i];
  29.             for (int j = i + 1; j < nums.length; j++) {
  30.                 int second = nums[j];
  31.                 sum = first + second;
  32.                 for (int num : nums) {
  33.                     if (num == sum) {
  34.                         System.out.printf("%d + %d = %d", first, second, sum);
  35.                         contains = true;
  36.                         break;
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.         if (!contains) {
  42.             System.out.println("No");
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement