Advertisement
Spocoman

09. Left and Right Sum

Aug 27th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class LeftAndRightSum {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int n = Integer.parseInt(scanner.nextLine());
  7.  
  8.         int leftSum = 0, rightSum = 0;
  9.  
  10.         for (int i = 0; i < n; i++) {
  11.             leftSum += Integer.parseInt(scanner.nextLine());
  12.         }
  13.  
  14.         for (int i = 0; i < n; i++) {
  15.             rightSum += Integer.parseInt(scanner.nextLine());
  16.         }
  17.  
  18.         if (leftSum == rightSum) {
  19.             System.out.printf("Yes, sum = %d\n", leftSum);
  20.         } else {
  21.             System.out.printf("No, diff = %d\n", Math.abs(leftSum - rightSum));
  22.         }
  23.     }
  24. }
  25.  
  26. ИЛИ:
  27.  
  28. import java.util.Scanner;
  29.  
  30. public class LeftAndRightSum {
  31.     public static void main(String[] args) {
  32.         Scanner scanner = new Scanner(System.in);
  33.         int n = Integer.parseInt(scanner.nextLine());
  34.  
  35.         int leftSum = 0, rightSum = 0;
  36.  
  37.         for (int i = -n; i < n; i++) {
  38.             if (i < 0) {
  39.                 leftSum += Integer.parseInt(scanner.nextLine());
  40.             } else {
  41.                 rightSum += Integer.parseInt(scanner.nextLine());
  42.             }
  43.         }
  44.        
  45.         if (leftSum == rightSum) {
  46.             System.out.printf("Yes, sum = %d\n", leftSum);
  47.         } else {
  48.             System.out.printf("No, diff = %d\n", Math.abs(leftSum - rightSum));
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement