desislava_topuzakova

8. Магически сбор (Magic Sum) - всички двойки числа

Jan 30th, 2022
1,318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. package Arrays;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class MagicSum_08 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String [] numbers = scanner.nextLine().split(" ");
  9.         //"1 7 6 2 19 23".split(" ") -> ["1", "7", "6", "2", "19", 23]
  10.         int magicSum = Integer.parseInt(scanner.nextLine());
  11.  
  12.         //всички двойки числа, които могат да се образуват
  13.         for (int index = 0; index < numbers.length - 1; index++) {
  14.                 int currentElement = Integer.parseInt(numbers[index]);
  15.                 for (int index1 = index + 1; index1 <= numbers.length - 1; index1++) {
  16.                     int nextElement = Integer.parseInt(numbers[index1]);
  17.                     if (currentElement + nextElement == magicSum) {
  18.                         System.out.println(currentElement + " " + nextElement);
  19.                     }
  20.                 }
  21.  
  22.         }
  23.     }
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment