Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MainClass {
- static void Time(long time){
- int Msecond = (int)(System.currentTimeMillis() - time);
- System.out.println("Total time spent:");
- System.out.println("Seconds: "+Msecond/1000+";");
- System.out.println("Milliseconds: "+Msecond+";");
- }
- //проверка массива
- static boolean check_mas(char Number1, int[] mas,int kol){
- kol = 0;//Что блять, почему тут ноль?
- if(Number1 == '1')return true;
- if(kol < 1)return false;
- if(Number1 == '2'){
- //для 4
- if(mas[8] == 4 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 3 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
- //для 3
- if(mas[8] == 3 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 2 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 1 && mas[7] == 2 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 2 && mas[7] == 0 && mas[6] == 1 && mas[5] == 0)return false;
- //для 2
- if(mas[8] == 2 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 1 && mas[7] == 0 && mas[6] == 1 && mas[5] == 0)return false;
- if(mas[8] == 0 && mas[7] == 2 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 1 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 1 && mas[7] == 0 && mas[6] == 1 && mas[5] == 0)return false;
- if(mas[8] == 0 && mas[7] == 1 && mas[6] == 1 && mas[5] == 0)return false;
- //для 1
- if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 0 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 0 && mas[7] == 0 && mas[6] == 1 && mas[5] == 0)return false;
- if(mas[8] == 0 && mas[7] == 0 && mas[6] == 0 && mas[5] == 1)return false;
- }else if(Number1 == '3'){
- //для 3
- if(mas[8] == 3 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- //для 2
- if(mas[8] == 2 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 1 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
- //для 1
- if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 0 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 0 && mas[7] == 0 && mas[6] == 1 && mas[5] == 0)return false;
- }else if(Number1 == '4'){
- //для 2
- if(mas[8] == 2 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- //для 1
- if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- if(mas[8] == 0 && mas[7] == 1 && mas[6] == 0 && mas[5] == 0)return false;
- }else if(Number1 == '5'){
- //для 1
- if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- }else if(Number1 == '6'){
- //для 1
- if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- }else if(Number1 == '7'){
- //для 1
- if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- }else if(Number1 == '8'){
- //для 1
- if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- }else if(Number1 == '9'){
- //для 1
- if(mas[8] == 1 && mas[7] == 0 && mas[6] == 0 && mas[5] == 0)return false;
- }
- return true;
- }
- //хранение данных
- static boolean Compliance_check(char Number1,char NumberCompliance){
- if(Number1 == '1'){
- if(NumberCompliance != '0')
- return true;
- }else if(Number1 == '2'){
- if((NumberCompliance == '5') ||
- (NumberCompliance == '6') ||
- (NumberCompliance == '7') ||
- (NumberCompliance == '8') ||
- (NumberCompliance == '9'))
- return true;
- }else if(Number1 == '3'){
- if((NumberCompliance == '6') ||
- (NumberCompliance == '7') ||
- (NumberCompliance == '8') ||
- (NumberCompliance == '9'))
- return true;
- }else if(Number1 == '4'){
- if((NumberCompliance == '7') ||
- (NumberCompliance == '8') ||
- (NumberCompliance == '9'))
- return true;
- }else if(Number1 == '5'){
- if((NumberCompliance == '8') ||
- (NumberCompliance == '9'))
- return true;
- }else if(Number1 == '6'){
- if((NumberCompliance == '8') ||
- (NumberCompliance == '9'))
- return true;
- }else if(Number1 == '7'){
- if((NumberCompliance == '8') ||
- (NumberCompliance == '9'))
- return true;
- }else if(Number1 == '8'){
- if((NumberCompliance == '8') ||
- (NumberCompliance == '9'))
- return true;
- }else if(Number1 == '9'){
- if((NumberCompliance == '8') ||
- (NumberCompliance == '9'))
- return true;
- }
- return false;
- }
- //Функция находит макс только одного числа пример: Ввод:1000 Вывод:999;Ввод:2998 Вывод:2998
- static String maxN(String Number,boolean n0){
- final char GL_J = Number.charAt(0);
- int pozition = -1;
- char J = 'N';
- char a,b = 'N';
- int[] mas = new int[10];
- int h;
- StringBuilder postNum = new StringBuilder();
- StringBuilder postNum1 = new StringBuilder();
- String Ez = GL_J+"";
- int kol = 0;
- h = Number.length()-1;
- for(int i = 1;i < Number.length();i++) {
- a = Number.charAt(i);
- if (Compliance_check(GL_J, a)) {
- if (pozition == -1) pozition = i;
- mas[a - '0']++;
- kol++;
- if (!check_mas(GL_J, mas, kol)) {
- if (kol >= 3) {
- a = Number.charAt(pozition);
- if (a == '8') {
- if (Number.charAt(pozition + 1) == '9') {
- int h1 = Number.length() - pozition - 2;
- for (int j = 0; j < h1; j++) postNum.append('9');
- return Number.substring(0, pozition + 1) + "8" + postNum;
- }
- }
- int h1 = Number.length() - pozition - 1;
- for (int j = 0; j < h1; j++) postNum.append('9');
- return Number.substring(0, pozition) + (char) (a-1)+ postNum;
- }
- } else break;
- }else break;
- }
- for(int i = 1;i < Number.length();i++){
- a = Number.charAt(i);
- if(J != 'N' ){
- if(!Compliance_check(J,a)){
- for (int j = i-1; j < h; j++) postNum.append("9");
- return Ez + J + "" + postNum;
- }
- }
- if(Compliance_check(GL_J,a)) {
- if(pozition == -1)pozition = i;
- if(Compliance_check(GL_J,(char)(a-1))){
- mas[a-'0'-1]++;
- if(!check_mas(GL_J,mas,kol+1)){
- if(b!='N')
- Ez+=b;
- J = (char)(a-1);
- }
- mas[a-'0'-1]--;
- }
- mas[a-'0']++;
- if(check_mas(GL_J,mas,kol)){
- if(n0) {
- if (GL_J - 1 - '0' != 0)
- postNum1.append((char) (GL_J - 1));
- }else
- postNum1.append((char)(GL_J-1));
- for (int j = 0; j < h; j++) postNum1.append("9");
- return postNum1+"";
- }
- kol++;
- }else{
- if(n0) {
- if (GL_J - 1 - '0' != 0)
- postNum1.append((char) (GL_J - 1));
- }else
- postNum1.append((char)(GL_J-1));
- for (int j = 0; j < h; j++) postNum1.append("9");
- return postNum1+"";
- }
- b = a;
- }
- return Number;
- }
- //функция сравнения
- static char Comparison(char a, char b){
- if(a == '0' && b == '1')return '1';
- else if(a == b)return a;
- else return 'N';
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- String lN = in.next();
- String lK = in.next();
- //long timeStart = System.currentTimeMillis();
- String MAX = "";
- int lenN = lN.length();
- int lenK = lK.length();
- if(lenK > lenN)System.out.println(maxN(lK,true));
- else{
- String s = "";
- char a,b,c;
- for(int i = 0;i < lenN;i++){
- a = lN.charAt(i);
- b = lK.charAt(i);
- c = Comparison(a,b);
- if(c == 'N'){
- s = lK.substring(i,lenK);
- if(i!=0)
- System.out.println(MAX.substring(0,i)+maxN(s,false));
- else
- System.out.println(MAX.substring(0,i)+maxN(s,true));
- //Time(timeStart);
- return;
- }else{
- MAX+=c+"";
- }
- }
- System.out.println(MAX);
- }
- //Time(timeStart);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement