Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. package lab_6;
  2.  
  3. public class Decoder {
  4.  
  5.     public static void main(String[] args)
  6.     {
  7.         String word = "����";
  8.         String result = "����";
  9.  
  10.         try {
  11.             Decoder.decode(word, result);
  12.  
  13.         } catch (Exception exc) {
  14.             System.out.println(exc);
  15.         }
  16.     }
  17.    
  18.     public static void decode(String word, String result)
  19.     {
  20.         int sum;
  21.         int count = 0;
  22.  
  23.         for (int firstIndex = 123; firstIndex < 987; firstIndex++) {
  24.  
  25.             for (int secondIndex = 123; secondIndex < 987; secondIndex ++)
  26.             {
  27.                 if (isRepeated(Integer.toString(firstIndex), Integer.toString(secondIndex))) {
  28.                     continue;
  29.                 }
  30.  
  31.                 sum = firstIndex + secondIndex;
  32.                
  33.                 if (sum > 9876) {
  34.                     continue;
  35.                 }
  36.                
  37.                 if (isRepeated(Integer.toString(firstIndex), Integer.toString(sum)))
  38.                 {
  39.                     continue;
  40.                 }
  41.                
  42.                 if (isRepeated(Integer.toString(secondIndex), Integer.toString(sum)))
  43.                 {
  44.                     continue;
  45.                 }
  46.                
  47.                 if (Integer.toString(firstIndex).split("")[0] == Integer.toString(sum).split("")[2]) {
  48.                     continue;
  49.                 }
  50.                
  51.                 count++;
  52.  
  53.                 System.out.println(firstIndex + " + " + secondIndex + " = " + sum);
  54.             }
  55.         }
  56.  
  57.         System.out.println("�������: " + count);
  58.     }
  59.    
  60.     private static boolean isRepeated(String firstString, String secondString)
  61.     {
  62.         String[] firstStringArray = firstString.split("");
  63.        
  64.         for (String firstLetter: firstStringArray)
  65.         {
  66.             if (!stringIsValid(firstString))
  67.             {
  68.                 return true;
  69.             }
  70.  
  71.             if (!stringIsValid(secondString))
  72.             {
  73.                 return true;
  74.             }
  75.            
  76.             if (secondString.indexOf(firstLetter) != -1)
  77.             {
  78.                 return true;
  79.             }
  80.         }
  81.  
  82.         return false;
  83.     }
  84.    
  85.     private static boolean stringIsValid(String string)
  86.     {
  87.         String[] letters = string.split("");
  88.         String substring;
  89.         int index = 0;
  90.  
  91.         while (index < letters.length - 1)
  92.         {
  93.             substring = string.substring(index);
  94.            
  95.             if (string.substring(index + 1).indexOf(letters[index]) != -1)
  96.             {
  97.                 return false;
  98.             }
  99.            
  100.             index++;
  101.         }
  102.        
  103.         return true;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement