Advertisement
roronoa

n chiffres et k chiffres distincts

Aug 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4.  
  5. class Solution {
  6.  
  7.     public static void main(String args[]) {
  8.         Scanner in = new Scanner(System.in);
  9.         int n = in.nextInt();
  10.         int m = in.nextInt();
  11.         //smallest
  12.         String k = "";
  13.         int i = 0;
  14.         // n digits
  15.         while(k.length() != n)
  16.         {
  17.              k = "";
  18.              k+= i;
  19.              i++;
  20.         }
  21.         i--;
  22.         // k distincts digits
  23.         while(allDistincts(k)!=true)
  24.         {
  25.             k = "";
  26.             i++;
  27.             k+=i;
  28.         }
  29.        
  30.         // max nombre avec 3 digits
  31.         String d = "";
  32.         int x = 0;
  33.         while(d.length() != n+1)
  34.         {
  35.             d = "";
  36.             d += x;
  37.             x++;
  38.         }
  39.        
  40.         x-=2;
  41.         String max = "";
  42.         max += x;
  43.         // k distincts digits
  44.         while(allDistincts(max)!=true)
  45.         {
  46.            
  47.             max = "";
  48.             x--;
  49.             max+=x;
  50.         }
  51.        
  52.         System.out.print(k);
  53.         System.out.print(" ");
  54.         System.out.println(max);
  55.     }
  56.     public static boolean allDistincts(String s)
  57.     {
  58.         boolean retour = true;
  59.         for(int i = 0; i < s.length(); i++)
  60.         {
  61.             char e = s.charAt(i);
  62.             for(int j = 0; j < s.length(); j++)
  63.             {
  64.                 if(j != i && e == s.charAt(j))
  65.                     retour = false;
  66.             }
  67.         }
  68.         return retour;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement