Guest User

Untitled

a guest
Sep 21st, 2014
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class MagicSumNew {
  6.  
  7.     public static void main(String[] args) {
  8.        
  9.         Scanner input = new Scanner(System.in);
  10.        
  11.         int D = Integer.parseInt(input.nextLine());
  12.         ArrayList<Integer> numbers = new ArrayList<>();
  13.        
  14.         while (true) {
  15.             String line = input.nextLine();
  16.            
  17.             if (line.equals("End")) {
  18.                 break;
  19.             }
  20.            
  21.             numbers.add(Integer.parseInt(line));
  22.         }
  23.        
  24.         long biggestSum = Integer.MIN_VALUE; //it was 0 in the exam unfortunately ...
  25.         int biggestA = 0;
  26.         int biggestB = 0;
  27.         int biggestC = 0;
  28.         boolean hasMagicNums = false;
  29.        
  30.         for (int i = 0; i < numbers.size(); i++) {
  31.             for (int j = i + 1; j < numbers.size(); j++) {
  32.                 for (int j2 = j + 1; j2 < numbers.size(); j2++) {
  33.                     int a = numbers.get(i).intValue();
  34.                     int b = numbers.get(j).intValue();
  35.                     int c = numbers.get(j2).intValue();
  36.                     long tempSum = 0;
  37.                     if ((a + b + c) % D == 0) {
  38.                         tempSum = a + b + c;
  39.                         hasMagicNums = true;
  40.                         if (tempSum > 0) {
  41.                             if (tempSum > biggestSum) {
  42.                                 biggestA = a;
  43.                                 biggestB = b;
  44.                                 biggestC = c;
  45.                                 biggestSum = tempSum;
  46.                             }
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.        
  52.         if (!hasMagicNums) {
  53.             System.out.println("No");
  54.         } else {
  55.             System.out.printf("(%d + %d + %d) %% %d = 0", biggestA, biggestB, biggestC, D);
  56.         }
  57.  
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment