desislava_topuzakova

04. Sum of Two Numbers

Feb 14th, 2022
1,368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. //Начин 1
  2. import java.util.Scanner;
  3.  
  4. public class SumOfTwoNumbers_04 {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         //x1 -> [startInterval; endInterval]
  8.         //x2 -> [startInterval; endInterval]
  9.         int startInterval = Integer.parseInt(scanner.nextLine());
  10.         int endInterval = Integer.parseInt(scanner.nextLine());
  11.         int magicNumber = Integer.parseInt(scanner.nextLine());
  12.  
  13.         int count = 0; //брой на комбинациите
  14.         //1. всички комбинации от 2 числа (х1 и х2)
  15.         for (int x1 = startInterval; x1 <= endInterval; x1++) {
  16.             for (int x2 = startInterval; x2 <= endInterval; x2++) {
  17.                 count++;
  18.                 //2. сумата от генерираната комбинация
  19.                 int sum = x1 + x2;
  20.                 //3. проверка дали комбинацията е валидна -> прекратявам програмата
  21.                 if (sum == magicNumber) {
  22.                     System.out.printf("Combination N:%d (%d + %d = %d)", count, x1, x2, sum);
  23.                     return; //прекратява изпълнението на цялата програма
  24.                 }
  25.             }
  26.         }
  27.  
  28.         //не е намерена комбинация
  29.         System.out.printf("%d combinations - neither equals %d", count, magicNumber);
  30.     }
  31. }
  32.  
  33. //Начин 2
  34. import java.util.Scanner;
  35.  
  36. public class SumOfTwoNumbers_04 {
  37.     public static void main(String[] args) {
  38.         Scanner scanner = new Scanner(System.in);
  39.         //x1 -> [startInterval; endInterval]
  40.         //x2 -> [startInterval; endInterval]
  41.         int startInterval = Integer.parseInt(scanner.nextLine());
  42.         int endInterval = Integer.parseInt(scanner.nextLine());
  43.         int magicNumber = Integer.parseInt(scanner.nextLine());
  44.  
  45.         int count = 0; //брой на комбинациите
  46.         boolean isFound = false;
  47.         //isFound = false -> не е открита комбинация
  48.         //isFound = true -> открита комбинация
  49.         //1. всички комбинации от 2 числа (х1 и х2)
  50.         for (int x1 = startInterval; x1 <= endInterval; x1++) {
  51.             for (int x2 = startInterval; x2 <= endInterval; x2++) {
  52.                 count++;
  53.                 //2. сумата от генерираната комбинация
  54.                 int sum = x1 + x2;
  55.                 //3. проверка дали комбинацията е валидна -> прекратявам програмата
  56.                 if (sum == magicNumber) {
  57.                     isFound = true;
  58.                     System.out.printf("Combination N:%d (%d + %d = %d)", count, x1, x2, sum);
  59.                     break; //прекратява цикъла, в който се намира
  60.                 }
  61.             }
  62.             if (isFound) {
  63.                 break;
  64.             }
  65.         }
  66.  
  67.         //не е намерена комбинация -> isFound = false
  68.         if (!isFound) { //isFound == false
  69.             System.out.printf("%d combinations - neither equals %d", count, magicNumber);
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment