Advertisement
Guest User

Untitled

a guest
May 1st, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.53 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MainClass {
  4.   static void Time(long time){
  5.     int Msecond = (int)(System.currentTimeMillis() - time);
  6.     System.out.println("Total time spent:");
  7.     System.out.println("Seconds: "+Msecond/1000+";");
  8.     System.out.println("Milliseconds: "+Msecond+";");
  9.   }
  10.   //проверка массива
  11.   static boolean check_mas(char Number1, int[] mas,int kol){
  12.     kol = 0;//Что блять, почему тут ноль?
  13.     if(Number1 == '1')return true;
  14.     if(kol < 1)return false;
  15.     if(Number1 == '2'){
  16.       //для 4
  17.       if(mas[8] == 4 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  18.       if(mas[8] == 3 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
  19.       //для 3
  20.       if(mas[8] == 3 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  21.       if(mas[8] == 2 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
  22.       if(mas[8] == 1 && mas[7] == 2 && mas[6] == 0 && mas[5] == 0)return false;
  23.       if(mas[8] == 2 && mas[7] == 0 && mas[6] == 1 && mas[5] == 0)return false;
  24.       //для 2
  25.       if(mas[8] == 2 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  26.       if(mas[8] == 1 && mas[7] == 0 && mas[6] == 1 && mas[5] == 0)return false;
  27.       if(mas[8] == 0 && mas[7] == 2 && mas[6] == 0 && mas[5] == 0)return false;
  28.       if(mas[8] == 1 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
  29.       if(mas[8] == 1 && mas[7] == 0 && mas[6] == 1 && mas[5] == 0)return false;
  30.       if(mas[8] == 0 && mas[7] == 1 && mas[6] == 1 && mas[5] == 0)return false;
  31.       //для 1
  32.       if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  33.       if(mas[8] == 0 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
  34.       if(mas[8] == 0 && mas[7] == 0 && mas[6] == 1 && mas[5] == 0)return false;
  35.       if(mas[8] == 0 && mas[7] == 0 && mas[6] == 0 && mas[5] == 1)return false;
  36.     }else if(Number1 == '3'){
  37.       //для 3
  38.       if(mas[8] == 3 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  39.       //для 2
  40.       if(mas[8] == 2 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  41.       if(mas[8] == 1 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
  42.       //для 1
  43.       if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  44.       if(mas[8] == 0 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
  45.       if(mas[8] == 0 && mas[7] == 0 && mas[6] == 1 && mas[5] == 0)return false;
  46.     }else if(Number1 == '4'){
  47.       //для 2
  48.       if(mas[8] == 2 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  49.       //для 1
  50.       if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  51.       if(mas[8] == 0 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
  52.     }else if(Number1 == '5'){
  53.       //для 1
  54.       if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  55.     }else if(Number1 == '6'){
  56.       //для 1
  57.       if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  58.     }else if(Number1 == '7'){
  59.       //для 1
  60.       if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  61.     }else if(Number1 == '8'){
  62.       //для 1
  63.       if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  64.     }else if(Number1 == '9'){
  65.       //для 1
  66.       if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
  67.     }
  68.     return true;
  69.   }
  70.   //хранение данных
  71.   static boolean Compliance_check(char Number1,char NumberCompliance){
  72.     if(Number1 == '1'){
  73.       if(NumberCompliance != '0')
  74.         return true;
  75.     }else if(Number1 == '2'){
  76.       if((NumberCompliance == '5') ||
  77.         (NumberCompliance == '6') ||
  78.         (NumberCompliance == '7') ||
  79.         (NumberCompliance == '8') ||
  80.         (NumberCompliance == '9'))
  81.         return true;
  82.     }else if(Number1 == '3'){
  83.       if((NumberCompliance == '6') ||
  84.         (NumberCompliance == '7') ||
  85.         (NumberCompliance == '8') ||
  86.         (NumberCompliance == '9'))
  87.         return true;
  88.     }else if(Number1 == '4'){
  89.       if((NumberCompliance == '7') ||
  90.         (NumberCompliance == '8') ||
  91.         (NumberCompliance == '9'))
  92.         return true;
  93.     }else if(Number1 == '5'){
  94.       if((NumberCompliance == '8') ||
  95.         (NumberCompliance == '9'))
  96.         return true;
  97.     }else if(Number1 == '6'){
  98.       if((NumberCompliance == '8') ||
  99.         (NumberCompliance == '9'))
  100.         return true;
  101.     }else if(Number1 == '7'){
  102.       if((NumberCompliance == '8') ||
  103.         (NumberCompliance == '9'))
  104.         return true;
  105.     }else if(Number1 == '8'){
  106.       if((NumberCompliance == '8') ||
  107.         (NumberCompliance == '9'))
  108.         return true;
  109.     }else if(Number1 == '9'){
  110.       if((NumberCompliance == '8') ||
  111.         (NumberCompliance == '9'))
  112.         return true;
  113.     }
  114.     return false;
  115.   }
  116.   //Функция находит макс только одного числа пример: Ввод:1000 Вывод:999;Ввод:2998 Вывод:2998
  117.   static String maxN(String Number,boolean n0){
  118.     final char GL_J = Number.charAt(0);
  119.     int pozition = -1;
  120.     char J = 'N';
  121.     char a,b = 'N';
  122.     int[] mas = new int[10];
  123.     int h;
  124.     StringBuilder postNum = new StringBuilder();
  125.     StringBuilder postNum1 = new StringBuilder();
  126.     String Ez = GL_J+"";
  127.     int kol = 0;
  128.     h = Number.length()-1;
  129.     for(int i = 1;i < Number.length();i++) {
  130.       a = Number.charAt(i);
  131.       if (Compliance_check(GL_J, a)) {
  132.         if (pozition == -1) pozition = i;
  133.         mas[a - '0']++;
  134.         kol++;
  135.         if (!check_mas(GL_J, mas, kol)) {
  136.           if (kol >= 3) {
  137.             a = Number.charAt(pozition);
  138.             if (a == '8') {
  139.               if (Number.charAt(pozition + 1) == '9') {
  140.                 int h1 = Number.length() - pozition - 2;
  141.                 for (int j = 0; j < h1; j++) postNum.append('9');
  142.                 return Number.substring(0, pozition + 1) + "8" + postNum;
  143.               }
  144.             }
  145.             int h1 = Number.length() - pozition - 1;
  146.             for (int j = 0; j < h1; j++) postNum.append('9');
  147.             return Number.substring(0, pozition) + (char) (a-1)+ postNum;
  148.  
  149.           }
  150.         } else break;
  151.       }else break;
  152.     }
  153.  
  154.  
  155.     for(int i = 1;i < Number.length();i++){
  156.       a = Number.charAt(i);
  157.       if(J != 'N' ){
  158.         if(!Compliance_check(J,a)){
  159.           for (int j = i-1; j < h; j++) postNum.append("9");
  160.           return Ez + J + "" + postNum;
  161.         }
  162.       }
  163.  
  164.       if(Compliance_check(GL_J,a)) {
  165.         if(pozition == -1)pozition = i;
  166.         if(Compliance_check(GL_J,(char)(a-1))){
  167.           mas[a-'0'-1]++;
  168.           if(!check_mas(GL_J,mas,kol+1)){
  169.             if(b!='N')
  170.               Ez+=b;
  171.             J = (char)(a-1);
  172.           }
  173.           mas[a-'0'-1]--;
  174.         }
  175.         mas[a-'0']++;
  176.         if(check_mas(GL_J,mas,kol)){
  177.           if(n0) {
  178.             if (GL_J - 1 - '0' != 0)
  179.               postNum1.append((char) (GL_J - 1));
  180.           }else
  181.             postNum1.append((char)(GL_J-1));
  182.           for (int j = 0; j < h; j++) postNum1.append("9");
  183.           return postNum1+"";
  184.         }
  185.         kol++;
  186.       }else{
  187.         if(n0) {
  188.           if (GL_J - 1 - '0' != 0)
  189.             postNum1.append((char) (GL_J - 1));
  190.         }else
  191.           postNum1.append((char)(GL_J-1));
  192.         for (int j = 0; j < h; j++) postNum1.append("9");
  193.         return postNum1+"";
  194.       }
  195.       b = a;
  196.     }
  197.  
  198.  
  199.     return Number;
  200.   }
  201.   //функция сравнения
  202.   static char Comparison(char a, char b){
  203.     if(a == '0' && b == '1')return '1';
  204.     else if(a == b)return a;
  205.     else return 'N';
  206.   }
  207.   public static void main(String[] args) {
  208.     Scanner in = new Scanner(System.in);
  209.     String lN = in.next();
  210.     String lK = in.next();
  211.     //long timeStart = System.currentTimeMillis();
  212.     String MAX = "";
  213.     int lenN = lN.length();
  214.     int lenK = lK.length();
  215.     if(lenK > lenN)System.out.println(maxN(lK,true));
  216.     else{
  217.       String s = "";
  218.       char a,b,c;
  219.       for(int i = 0;i < lenN;i++){
  220.         a = lN.charAt(i);
  221.         b = lK.charAt(i);
  222.         c = Comparison(a,b);
  223.         if(c == 'N'){
  224.           s = lK.substring(i,lenK);
  225.           if(i!=0)
  226.             System.out.println(MAX.substring(0,i)+maxN(s,false));
  227.           else
  228.             System.out.println(MAX.substring(0,i)+maxN(s,true));
  229.  
  230.           //Time(timeStart);
  231.  
  232.           return;
  233.         }else{
  234.           MAX+=c+"";
  235.         }
  236.       }
  237.       System.out.println(MAX);
  238.     }
  239.  
  240.     //Time(timeStart);
  241.  
  242.   }
  243.  
  244. }
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement