홀수와 짝수의 합 차이가 있는 모든 n자리 숫자를 1로 인쇄합니다.

정수가 주어지면 N 자릿수를 나타냅니다. 임무는 모두 인쇄하는 것입니다. n자리 숫자 짝수 위치와 홀수 위치의 숫자 합 간의 절대 차이는 정확히 다음과 같습니다. 1 .
메모 : 숫자는 다음으로 시작하면 안 됩니다. (선행 0은 허용되지 않습니다.)

예:  

입력 : n = 2
산출 : 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98

입력 : n = 3
산출 : 100 111 120 122 131 133 142 144 153 155 164 166 175 177 186
188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285
287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395
397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560
562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760
771 780 782 791 793 870 881 890 892 980 991  

[예상 접근 방식] 재귀를 활용

아이디어는 재귀적으로 모든 n자리 숫자를 생성하는 동안 합계 추적 의 자릿수 심지어 그리고 이상한 두 개의 변수를 사용하여 위치를 지정합니다. 주어진 위치에 대해 0부터 9까지의 모든 숫자로 채우고 현재 위치가 짝수인지 홀수인지에 따라 짝수 또는 홀수 합계를 증가시킵니다. 선행 0의 경우는 숫자로 계산되지 않으므로 별도로 처리합니다.
우리는 배열 인덱스처럼 0 기반 번호 매기기를 따랐습니다. 즉, 앞의(가장 왼쪽) 숫자는 짝수 위치에 있는 것으로 간주되고 그 옆의 숫자는 홀수 위치에 있는 것으로 간주됩니다.

C++
   // C++ program to print all n-digit numbers such that   // the absolute difference between the sum of digits at   // even and odd positions is 1   #include          using     namespace     std  ;   // Recursive function to generate numbers   void     findNDigitNumsUtil  (  int     pos       int     n       int     num        int     evenSum       int     oddSum        vector   <  int  >     &  res  )     {      // If number is formed      if     (  pos     ==     n  )     {      // Check absolute difference condition      if     (  abs  (  evenSum     -     oddSum  )     ==     1  )     {      res  .  push_back  (  num  );      }      return  ;      }      // Digits to consider at current position      for     (  int     d     =     0  ;     d      <=     9  ;     d  ++  )     {      // Skip leading 0      if     (  pos     ==     0     &&     d     ==     0  )     {      continue  ;      }      // If position is even (0-based) add to evenSum      if     (  pos     %     2     ==     0  )     {      findNDigitNumsUtil  (  pos     +     1       n       num     *     10     +     d        evenSum     +     d       oddSum       res  );      }      // If position is odd add to oddSum      else     {      findNDigitNumsUtil  (  pos     +     1       n       num     *     10     +     d        evenSum       oddSum     +     d       res  );      }      }   }   // Function to prepare and collect valid numbers   vector   <  int  >     findNDigitNums  (  int     n  )     {          vector   <  int  >     res  ;      findNDigitNumsUtil  (  0       n       0       0       0       res  );          return     res  ;   }   // Driver code   int     main  ()     {      int     n     =     2  ;      vector   <  int  >     res     =     findNDigitNums  (  n  );      for     (  int     i     =     0  ;     i      <     res  .  size  ();     i  ++  )     {      cout      < <     res  [  i  ]      < <     ' '  ;      }      return     0  ;   }   
Java
   // Java program to print all n-digit numbers such that   // the absolute difference between the sum of digits at   // even and odd positions is 1   import     java.util.*  ;   class   GfG     {      // Recursive function to generate numbers      static     void     findNDigitNumsUtil  (  int     pos       int     n       int     num        int     evenSum       int     oddSum        ArrayList   <  Integer  >     res  )     {      // If number is formed      if     (  pos     ==     n  )     {      // Check absolute difference condition      if     (  Math  .  abs  (  evenSum     -     oddSum  )     ==     1  )     {      res  .  add  (  num  );      }      return  ;      }      // Digits to consider at current position      for     (  int     d     =     0  ;     d      <=     9  ;     d  ++  )     {      // Skip leading 0      if     (  pos     ==     0     &&     d     ==     0  )     {      continue  ;      }      // If position is even (0-based) add to evenSum      if     (  pos     %     2     ==     0  )     {      findNDigitNumsUtil  (  pos     +     1       n       num     *     10     +     d        evenSum     +     d       oddSum       res  );      }      // If position is odd add to oddSum      else     {      findNDigitNumsUtil  (  pos     +     1       n       num     *     10     +     d        evenSum       oddSum     +     d       res  );      }      }      }      // Function to prepare and collect valid numbers      static     ArrayList   <  Integer  >     findNDigitNums  (  int     n  )     {      ArrayList   <  Integer  >     res     =     new     ArrayList   <>  ();      findNDigitNumsUtil  (  0       n       0       0       0       res  );      return     res  ;      }      // Driver code      public     static     void     main  (  String  []     args  )     {      int     n     =     2  ;      ArrayList   <  Integer  >     res     =     findNDigitNums  (  n  );      // Print all collected valid numbers      for     (  int     i     =     0  ;     i      <     res  .  size  ();     i  ++  )     {      System  .  out  .  print  (  res  .  get  (  i  )     +     ' '  );      }      }   }   
Python
   # Python program to print all n-digit numbers such that   # the absolute difference between the sum of digits at   # even and odd positions is 1   # Recursive function to generate numbers   def   findNDigitNumsUtil  (  pos     n     num     evenSum     oddSum     res  ):   # If number is formed   if   pos   ==   n  :   # Check absolute difference condition   if   abs  (  evenSum   -   oddSum  )   ==   1  :   res  .  append  (  num  )   return   # Digits to consider at current position   for   d   in   range  (  10  ):   # Skip leading 0   if   pos   ==   0   and   d   ==   0  :   continue   # If position is even (0-based) add to evenSum   if   pos   %   2   ==   0  :   findNDigitNumsUtil  (  pos   +   1     n     num   *   10   +   d     evenSum   +   d     oddSum     res  )   # If position is odd add to oddSum   else  :   findNDigitNumsUtil  (  pos   +   1     n     num   *   10   +   d     evenSum     oddSum   +   d     res  )   # Function to prepare and collect valid numbers   def   findNDigitNums  (  n  ):   res   =   []   findNDigitNumsUtil  (  0     n     0     0     0     res  )   return   res   # Driver code   if   __name__   ==   '__main__'  :   n   =   2   res   =   findNDigitNums  (  n  )   # Print all collected valid numbers   for   i   in   range  (  len  (  res  )):   print  (  res  [  i  ]   end  =  ' '  )   
C#
   // C# program to print all n-digit numbers such that   // the absolute difference between the sum of digits at   // even and odd positions is 1   using     System  ;   using     System.Collections.Generic  ;   class     GfG     {      // Recursive function to generate numbers      static     void     findNDigitNumsUtil  (  int     pos       int     n       int     num        int     evenSum       int     oddSum        List   <  int  >     res  )     {      // If number is formed      if     (  pos     ==     n  )     {      // Check absolute difference condition      if     (  Math  .  Abs  (  evenSum     -     oddSum  )     ==     1  )     {      res  .  Add  (  num  );      }      return  ;      }      // Digits to consider at current position      for     (  int     d     =     0  ;     d      <=     9  ;     d  ++  )     {      // Skip leading 0      if     (  pos     ==     0     &&     d     ==     0  )     {      continue  ;      }      // If position is even (0-based) add to evenSum      if     (  pos     %     2     ==     0  )     {      findNDigitNumsUtil  (  pos     +     1       n       num     *     10     +     d        evenSum     +     d       oddSum       res  );      }      // If position is odd add to oddSum      else     {      findNDigitNumsUtil  (  pos     +     1       n       num     *     10     +     d        evenSum       oddSum     +     d       res  );      }      }      }      // Function to prepare and collect valid numbers      static     List   <  int  >     findNDigitNums  (  int     n  )     {      List   <  int  >     res     =     new     List   <  int  >  ();      findNDigitNumsUtil  (  0       n       0       0       0       res  );      return     res  ;      }      // Driver code      public     static     void     Main  (  string  []     args  )     {      int     n     =     2  ;      List   <  int  >     res     =     findNDigitNums  (  n  );      // Print all collected valid numbers      for     (  int     i     =     0  ;     i      <     res  .  Count  ;     i  ++  )     {      Console  .  Write  (  res  [  i  ]     +     ' '  );      }      }   }   
JavaScript
   // JavaScript program to print all n-digit numbers such that   // the absolute difference between the sum of digits at   // even and odd positions is 1   // Recursive function to generate numbers   function     findNDigitNumsUtil  (  pos       n       num       evenSum       oddSum       res  )     {      // If number is formed      if     (  pos     ===     n  )     {      // Check absolute difference condition      if     (  Math  .  abs  (  evenSum     -     oddSum  )     ===     1  )     {      res  .  push  (  num  );      }      return  ;      }      // Digits to consider at current position      for     (  let     d     =     0  ;     d      <=     9  ;     d  ++  )     {      // Skip leading 0      if     (  pos     ===     0     &&     d     ===     0  )     {      continue  ;      }      // If position is even (0-based) add to evenSum      if     (  pos     %     2     ===     0  )     {      findNDigitNumsUtil  (  pos     +     1       n       num     *     10     +     d        evenSum     +     d       oddSum       res  );      }      // If position is odd add to oddSum      else     {      findNDigitNumsUtil  (  pos     +     1       n       num     *     10     +     d        evenSum       oddSum     +     d       res  );      }      }   }   // Function to prepare and collect valid numbers   function     findNDigitNums  (  n  )     {      let     res     =     [];      findNDigitNumsUtil  (  0       n       0       0       0       res  );      return     res  ;   }   // Driver code   let     n     =     2  ;   let     res     =     findNDigitNums  (  n  );   // Print all collected valid numbers   for     (  let     i     =     0  ;     i      <     res  .  length  ;     i  ++  )     {      process  .  stdout  .  write  (  res  [  i  ]     +     ' '  );   }   

산출
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98  

시간 복잡도: 각 숫자에는 최대 10개의 선택 항목이 있으므로 O(9 × 10^(n-1))입니다(첫 번째 숫자는 9개 제외).
공간 복잡도: O(n + k) 여기서 n은 재귀 깊이이고 k는 유효한 결과 수입니다.