Pienin määrä tietyllä numerolla ja summalla

Pienin määrä tietyllä numerolla ja summalla
Kokeile sitä GFG -harjoituksessa

Annetaan kaksi kokonaislukua s ja d -d löytää pienin Mahdollinen numero, joka on tarkalleen D -numerot ja numeroiden summa yhtäpitävä s .
Palauta numero a naru . Jos tällaista numeroa ei ole, palaa '-1' .

Esimerkkejä:

Tulo: s = 9 d = 2
Lähtö: 18
Selitys: 18 on pienin lukumäärä, jonka summa on = 9 ja kokonais numerot = 2.

Tulo: s = 20 d = 3
Lähtö: 299
Selitys: 299 on pienin määrä mahdollisimman paljon numeroita = 20 ja kokonais numerot = 3.

Tulo: s = 1 d = 1
Lähtö: 1
Selitys: 1 on pienin lukumäärä, jonka avulla numeroiden summa = 1 ja kokonais numerot = 1.

Sisältötaulukko

[Brute -Force -lähestymistapa] iteroi peräkkäin - o (d*(10^d)) aika ja o (1) tila

Koska numerot ovat peräkkäisiä raa'an voimanlähestymistapa iteroi pienin D-dogit-numero suurin tarkistaa jokainen. Jokaisesta numerosta lasketaan sen numeroita ja palauta ensimmäinen kelvollinen ottelu, joka varmistaa, että pienin mahdollinen luku on valittu. Jos voimassa olevaa numeroa ei ole, palautamme '-1' .

C++
   // C++ program to find the smallest d-digit   // number with the given sum using    // a brute force approach   #include          using     namespace     std  ;   string     smallestNumber  (  int     s       int     d  )     {          // The smallest d-digit number is 10^(d-1)      int     start     =     pow  (  10       d     -     1  );          // The largest d-digit number is 10^d - 1      int     end     =     pow  (  10       d  )     -     1  ;      // Iterate through all d-digit numbers      for     (  int     num     =     start  ;     num      <=     end  ;     num  ++  )     {          int     sum     =     0       x     =     num  ;      // Calculate sum of digits      while     (  x     >     0  )     {      sum     +=     x     %     10  ;      x     /=     10  ;      }      // If sum matches return the number      // as a string      if     (  sum     ==     s  )     {      return     to_string  (  num  );      }      }      // If no valid number is found return '-1'      return     '-1'  ;   }   // Driver Code   int     main  ()     {          int     s     =     9       d     =     2  ;          cout      < <     smallestNumber  (  s       d  )      < <     endl  ;      return     0  ;   }   
Java
   // Java program to find the smallest d-digit   // number with the given sum using    // a brute force approach   import     java.util.*  ;   class   GfG     {          static     String     smallestNumber  (  int     s       int     d  )     {          // The smallest d-digit number is 10^(d-1)      int     start     =     (  int  )     Math  .  pow  (  10       d     -     1  );          // The largest d-digit number is 10^d - 1      int     end     =     (  int  )     Math  .  pow  (  10       d  )     -     1  ;      // Iterate through all d-digit numbers      for     (  int     num     =     start  ;     num      <=     end  ;     num  ++  )     {          int     sum     =     0       x     =     num  ;      // Calculate sum of digits      while     (  x     >     0  )     {      sum     +=     x     %     10  ;      x     /=     10  ;      }      // If sum matches return the number      // as a string      if     (  sum     ==     s  )     {      return     Integer  .  toString  (  num  );      }      }      // If no valid number is found return '-1'      return     '-1'  ;      }      // Driver Code      public     static     void     main  (  String  []     args  )     {          int     s     =     9       d     =     2  ;          System  .  out  .  println  (  smallestNumber  (  s       d  ));      }   }   
Python
   # Python program to find the smallest d-digit   # number with the given sum using    # a brute force approach   def   smallestNumber  (  s     d  ):   # The smallest d-digit number is 10^(d-1)   start   =   10  **  (  d   -   1  )   # The largest d-digit number is 10^d - 1   end   =   10  **  d   -   1   # Iterate through all d-digit numbers   for   num   in   range  (  start     end   +   1  ):   sum_digits   =   0   x   =   num   # Calculate sum of digits   while   x   >   0  :   sum_digits   +=   x   %   10   x   //=   10   # If sum matches return the number   # as a string   if   sum_digits   ==   s  :   return   str  (  num  )   # If no valid number is found return '-1'   return   '-1'   # Driver Code   if   __name__   ==   '__main__'  :   s     d   =   9     2   print  (  smallestNumber  (  s     d  ))   
C#
   // C# program to find the smallest d-digit   // number with the given sum using    // a brute force approach   using     System  ;   class     GfG     {          static     string     smallestNumber  (  int     s       int     d  )     {          // The smallest d-digit number is 10^(d-1)      int     start     =     (  int  )  Math  .  Pow  (  10       d     -     1  );          // The largest d-digit number is 10^d - 1      int     end     =     (  int  )  Math  .  Pow  (  10       d  )     -     1  ;      // Iterate through all d-digit numbers      for     (  int     num     =     start  ;     num      <=     end  ;     num  ++  )     {          int     sum     =     0       x     =     num  ;      // Calculate sum of digits      while     (  x     >     0  )     {      sum     +=     x     %     10  ;      x     /=     10  ;      }      // If sum matches return the number      // as a string      if     (  sum     ==     s  )     {      return     num  .  ToString  ();      }      }      // If no valid number is found return '-1'      return     '-1'  ;      }      // Driver Code      public     static     void     Main  ()     {          int     s     =     9       d     =     2  ;          Console  .  WriteLine  (  smallestNumber  (  s       d  ));      }   }   
JavaScript
   // JavaScript program to find the smallest d-digit   // number with the given sum using    // a brute force approach   function     smallestNumber  (  s       d  )     {          // The smallest d-digit number is 10^(d-1)      let     start     =     Math  .  pow  (  10       d     -     1  );          // The largest d-digit number is 10^d - 1      let     end     =     Math  .  pow  (  10       d  )     -     1  ;      // Iterate through all d-digit numbers      for     (  let     num     =     start  ;     num      <=     end  ;     num  ++  )     {          let     sum     =     0       x     =     num  ;      // Calculate sum of digits      while     (  x     >     0  )     {      sum     +=     x     %     10  ;      x     =     Math  .  floor  (  x     /     10  );      }      // If sum matches return the number      // as a string      if     (  sum     ===     s  )     {      return     num  .  toString  ();      }      }      // If no valid number is found return '-1'      return     '-1'  ;   }   // Driver Code   let     s     =     9       d     =     2  ;   console  .  log  (  smallestNumber  (  s       d  ));   

Tulos
18  

[Odotettu lähestymistapa] Greedy -tekniikan käyttäminen - O (D) -aika ja O (1) -tilaa

Lähestymistapa varmistaa vasemman numeron ei ole nolla niin me varaa 1 ja jaa jäljellä oleva summa vasemmalle Pienimman mahdollisen numeron muodostamiseksi. Se ahne lähestymistapa auttaa sijoittamaan suurimmat mahdolliset arvot (jopa 9) oikeanpuoleiset asennot pitää numeron pienenä.

Vaiheet yllä olevan idean toteuttamiseksi:

  • Tarkista rajoitukset a Voimassa oleva summa s voidaan muodostaa käyttämällä D -numerot muuten palata '-1' .
  • Alustaa tulos kuin D '0 ja varaa 1 siihen vasen numero vähentämällä s 1: llä .
  • Poiketa jstk vasemmalle ja aseta suurin mahdollinen numero ( <= 9) Päivitettäessä s Vastaavasti.
  • Jos s <= 9 Aseta arvo nykyiseen sijaintiin ja aseta s = 0 Lopettaa päivitykset.
  • Määrittää vasen numero lisäämällä jäljellä varmistaa, että se pysyy nolla- .
  • Muuntaa tulos merkkijono vaadittavaan muotoon ja palata Se lopullisena lähtönä.
C++
   // C++ program to find the smallest d-digit    // number with the given sum using   // Greedy Technique   #include          using     namespace     std  ;   string     smallestNumber  (  int     s       int     d  )     {          // If sum is too small or too large       // for d digits      if     (  s      <     1     ||     s     >     9     *     d  )     {      return     '-1'  ;      }      string     result  (  d       '0'  );             // Reserve 1 for the leftmost digit      s  --  ;         // Fill digits from right to left      for     (  int     i     =     d     -     1  ;     i     >     0  ;     i  --  )     {          // Place the largest possible value  <= 9      if     (  s     >     9  )     {      result  [  i  ]     =     '9'  ;      s     -=     9  ;      }     else     {      result  [  i  ]     =     '0'     +     s  ;      s     =     0  ;      }      }      // Place the leftmost digit ensuring      // it's non-zero      result  [  0  ]     =     '1'     +     s  ;          return     result  ;   }   // Driver Code   int     main  ()     {          int     s     =     9       d     =     2  ;          cout      < <     smallestNumber  (  s       d  )      < <     endl  ;      return     0  ;   }   
Java
   // Java program to find the smallest d-digit    // number with the given sum using   // Greedy Technique   import     java.util.*  ;   class   GfG     {          static     String     smallestNumber  (  int     s       int     d  )     {          // If sum is too small or too large       // for d digits      if     (  s      <     1     ||     s     >     9     *     d  )     {      return     '-1'  ;      }      char  []     result     =     new     char  [  d  ]  ;      Arrays  .  fill  (  result       '0'  );          // Reserve 1 for the leftmost digit      s  --  ;      // Fill digits from right to left      for     (  int     i     =     d     -     1  ;     i     >     0  ;     i  --  )     {          // Place the largest possible value  <= 9      if     (  s     >     9  )     {      result  [  i  ]     =     '9'  ;      s     -=     9  ;      }     else     {      result  [  i  ]     =     (  char  )     (  '0'     +     s  );      s     =     0  ;      }      }      // Place the leftmost digit ensuring      // it's non-zero      result  [  0  ]     =     (  char  )     (  '1'     +     s  );          return     new     String  (  result  );      }      // Driver Code      public     static     void     main  (  String  []     args  )     {          int     s     =     9       d     =     2  ;          System  .  out  .  println  (  smallestNumber  (  s       d  ));      }   }   
Python
   # Python program to find the smallest d-digit    # number with the given sum using   # Greedy Technique   def   smallestNumber  (  s     d  ):   # If sum is too small or too large    # for d digits   if   s    <   1   or   s   >   9   *   d  :   return   '-1'   result   =   [  '0'  ]   *   d   # Reserve 1 for the leftmost digit   s   -=   1   # Fill digits from right to left   for   i   in   range  (  d   -   1     0     -  1  ):   # Place the largest possible value  <= 9   if   s   >   9  :   result  [  i  ]   =   '9'   s   -=   9   else  :   result  [  i  ]   =   str  (  s  )   s   =   0   # Place the leftmost digit ensuring   # it's non-zero   result  [  0  ]   =   str  (  1   +   s  )   return   ''  .  join  (  result  )   # Driver Code   if   __name__   ==   '__main__'  :   s     d   =   9     2   print  (  smallestNumber  (  s     d  ))   
C#
   // C# program to find the smallest d-digit    // number with the given sum using   // Greedy Technique   using     System  ;   class     GfG     {      static     string     smallestNumber  (  int     s       int     d  )     {          // If sum is too small or too large       // for d digits      if     (  s      <     1     ||     s     >     9     *     d  )     {      return     '-1'  ;      }      char  []     result     =     new     char  [  d  ];      Array  .  Fill  (  result       '0'  );      // Reserve 1 for the leftmost digit      s  --  ;      // Fill digits from right to left      for     (  int     i     =     d     -     1  ;     i     >     0  ;     i  --  )     {          // Place the largest possible value  <= 9      if     (  s     >     9  )     {      result  [  i  ]     =     '9'  ;      s     -=     9  ;      }     else     {      result  [  i  ]     =     (  char  )     (  '0'     +     s  );      s     =     0  ;      }      }      // Place the leftmost digit ensuring      // it's non-zero      result  [  0  ]     =     (  char  )     (  '1'     +     s  );          return     new     string  (  result  );      }      // Driver Code      static     void     Main  ()     {          int     s     =     9       d     =     2  ;          Console  .  WriteLine  (  smallestNumber  (  s       d  ));      }   }   
JavaScript
   // JavaScript program to find the smallest d-digit    // number with the given sum using   // Greedy Technique   function     smallestNumber  (  s       d  )     {          // If sum is too small or too large       // for d digits      if     (  s      <     1     ||     s     >     9     *     d  )     {      return     '-1'  ;      }      let     result     =     Array  (  d  ).  fill  (  '0'  );         // Reserve 1 for the leftmost digit      s  --  ;      // Fill digits from right to left      for     (  let     i     =     d     -     1  ;     i     >     0  ;     i  --  )     {          // Place the largest possible value  <= 9      if     (  s     >     9  )     {      result  [  i  ]     =     '9'  ;      s     -=     9  ;      }     else     {      result  [  i  ]     =     String  (  s  );      s     =     0  ;      }      }      // Place the leftmost digit ensuring      // it's non-zero      result  [  0  ]     =     String  (  1     +     s  );          return     result  .  join  (  ''  );   }   // Driver Code   let     s     =     9       d     =     2  ;   console  .  log  (  smallestNumber  (  s       d  ));   

Tulos
18