Venciity

[SoftUni Java] ExamPreparation 04.FiveSpecialLetters

May 24th, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class _04_FiveSpecialLetters {
  5.  
  6.     public static void main(String[] args) {
  7.         @SuppressWarnings("resource")
  8.         Scanner input = new Scanner(System.in);
  9.  
  10.         int startNum = Integer.parseInt(input.nextLine());
  11.         int endNum = Integer.parseInt(input.nextLine());
  12.  
  13.         boolean resultFound = false;
  14.        
  15.         for (char first = 'a'; first <= 'e'; first++) {
  16.             for (char second = 'a'; second <= 'e'; second++) {
  17.                 for (char third = 'a'; third <= 'e'; third++) {
  18.                     for (char fourth = 'a'; fourth <= 'e'; fourth++) {
  19.                         for (char fifth = 'a'; fifth <= 'e'; fifth++) {
  20.                             String wholeWord = "" + first + second + third + fourth + fifth;
  21.                             long weight = calcWeight(wholeWord);
  22.                            
  23.                             if (weight >= startNum && weight <= endNum) {
  24.                                 resultFound = true;
  25.                                 System.out.print(wholeWord + " ");
  26.                             }
  27.                         }
  28.                     }
  29.                 }
  30.             }
  31.         }
  32.        
  33.         if (!resultFound) {
  34.             System.out.println("No");
  35.         }
  36.     }
  37.  
  38.     private static long calcWeight(String word) {
  39.         long weight = 0;
  40.         int index = 1;
  41.         boolean[] visited = new boolean[(int)'e' + 1];
  42.        
  43.         for (int i = 0; i < word.length(); i++) {
  44.             char currentLetter = word.charAt(i);
  45.            
  46.             if (!visited[currentLetter]) {
  47.                 weight += index * calcWeight(currentLetter);
  48.                 visited[currentLetter] = true;
  49.                 index++;
  50.             }
  51.         }
  52.         return weight;
  53.     }
  54.  
  55.     private static int calcWeight(char letter) {
  56.         switch (letter) {
  57.         case 'a':
  58.             return 5;
  59.         case 'b':
  60.             return -12;
  61.         case 'c':
  62.             return 47;
  63.         case 'd':
  64.             return 7;
  65.         case 'e':
  66.             return -32;
  67.         }
  68.        
  69.         return 0;
  70.     }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment