Advertisement
dimipan80

C#Exams 2. Pairs (on Java Code)

Aug 22nd, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _2_Pairs {
  4.  
  5.     public static void main(String[] args) {
  6.         // TODO Auto-generated method stub
  7.         Scanner scan = new Scanner(System.in);
  8.         String inputLine = scan.nextLine();
  9.  
  10.         String[] numStr = inputLine.split("[ ]+");
  11.         int previousSum = Integer.parseInt(numStr[0])
  12.                 + Integer.parseInt(numStr[1]);
  13.         int maxDiff = 0;
  14.         for (int i = 2; i + 1 < numStr.length; i += 2) {
  15.             int nextSum = Integer.parseInt(numStr[i])
  16.                     + Integer.parseInt(numStr[i + 1]);
  17.             int currentDiff = Math.abs(nextSum - previousSum);
  18.             maxDiff = Math.max(maxDiff, currentDiff);
  19.             previousSum = nextSum;
  20.         }
  21.  
  22.         if (maxDiff == 0) {
  23.             System.out.println("Yes, value=" + previousSum);
  24.         } else {
  25.             System.out.println("No, maxdiff=" + maxDiff);
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement