Advertisement
dimipan80

Exam 4. Magic Car Numbers (on Java code)

Sep 10th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _4_MagicCarNumbers {
  4.  
  5.     private static char[] letters = { 'A', 'B', 'C', 'E', 'H', 'K', 'M', 'P',
  6.             'T', 'X' };
  7.     private static int[] weights = { 10, 20, 30, 50, 80, 110, 130, 160, 200,
  8.             240 };
  9.  
  10.     public static void main(String[] args) {
  11.         // TODO Auto-generated method stub
  12.         Scanner scan = new Scanner(System.in);
  13.         int magicWeight = scan.nextInt();
  14.  
  15.         int countMagicCarNumbers = 0;
  16.         if (magicWeight >= 60 && magicWeight <= 556) {
  17.             magicWeight -= 40;
  18.             countMagicCarNumbers = getCountOfMagicCarNumbers(magicWeight);
  19.         }
  20.  
  21.         System.out.println(countMagicCarNumbers);
  22.     }
  23.  
  24.     private static int getCountOfMagicCarNumbers(int magicWeight) {
  25.         int counter = 0;
  26.         int d1, d2, d3, d4, let1, let2;
  27.         for (d1 = 0; d1 <= 9; d1++) {
  28.             for (d2 = 0; d2 <= 9; d2++) {
  29.                 for (d3 = 0; d3 <= 9; d3++) {
  30.                     for (d4 = 0; d4 <= 9; d4++) {
  31.                         for (let1 = 0; let1 < letters.length; let1++) {
  32.                             for (let2 = 0; let2 < letters.length; let2++) {
  33.                                 int specialWeight = calculateTheWeightOfSpecialMagicNumbers(
  34.                                         d1, d2, d3, d4, let1, let2);
  35.                                 if (specialWeight == magicWeight) {
  36.                                     counter++;
  37.                                 }
  38.                             }
  39.                         }
  40.                     }
  41.                 }
  42.             }
  43.         }
  44.  
  45.         return counter;
  46.     }
  47.  
  48.     private static int calculateTheWeightOfSpecialMagicNumbers(int a, int b,
  49.             int c, int d, int x, int y) {
  50.         boolean numberIsSpecial = (b == a && c == a && d == a)
  51.                 || (b != a && c == b && d == b) || (b == a && c == a && d != a)
  52.                 || (b == a && c != a && d == c) || (b != a && c == a && d == b)
  53.                 || (b != a && c == b && d == a);
  54.  
  55.         int weight = 0;
  56.         if (numberIsSpecial) {
  57.             weight = a + b + c + d + weights[x] + weights[y];
  58.         }
  59.  
  60.         return weight;
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement