📜  词典索引上最小的排列,原始索引没有数字

📅  最后修改于: 2021-04-22 01:20:31             🧑  作者: Mango

给定一个整数N的任务是找到形式的整数的字典序最小排列:12345 … N使得没有数字的指数发生在原来的号码,也就是说,如果P 1个P 2 P 3 … P N是我们的排列,则P i不能等于i。

注意:N大于1且小于10。

例子

Input : N = 5
Output : 21435

Input : N = 2
Output : 21

对于最小的排列,应将较小的数字放在开头。因此,有两种情况可以解决此问题。

  1. N为偶数,即位数为偶数。在这种情况下,如果所有奇数位都放置在下一个偶数索引中,而所有偶数位都放置在其前一个索引中,则满足上述条件的排列将最小。
  2. N为奇数,即位数为奇数。在这种情况下,所有情况都与上述情况类似,唯一的变化是,最后三位数字的排列方式是随机的,以使它们的排列最小。例如,如果我们的后三位数字为123,则231是可能的最小排列。

算法

  • 如果N是偶数:
    • 将所有偶数数字(最多N个)按升序排列在奇数索引处。
    • 将所有奇数数字按升序排列在偶数索引处。
  • 别的
    • 将所有偶数数字(最多N-3)按升序排列在奇数索引处。
    • 将所有奇数位(最多N-4)按升序排列在偶数索引处。
    • 将N放在第(N-1)位,将N-1放在第(N-2)位,将N-2放在第N位。

      下面是上述方法的实现:

      C++
      // C++ program to find the smallest permutaion
        
      #include 
      using namespace std;
        
      // Function to print the smallest permutation
      string smallestPermute(int n)
      {
          char res[n + 1];
        
          // when n is even
          if (n % 2 == 0) {
              for (int i = 0; i < n; i++) {
                  if (i % 2 == 0)
                      res[i] = 48 + i + 2;
                  else
                      res[i] = 48 + i;
              }
          }
        
          // when n is odd
          else {
              for (int i = 0; i < n - 2; i++) {
                  if (i % 2 == 0)
                      res[i] = 48 + i + 2;
                  else
                      res[i] = 48 + i;
              }
              // handling last 3 digit
              res[n - 1] = 48 + n - 2;
              res[n - 2] = 48 + n;
              res[n - 3] = 48 + n - 1;
          }
        
          // add EOL and print result
          res[n] = '\0';
        
          return res;
      }
        
      // Driver Code
      int main()
      {
          int n = 7;
        
          cout << smallestPermute(n);
        
          return 0;
      }


      Java
      // Java program to find the smallest permutaion
      class GFG
      {
        
      // Function to print the smallest permutation
      static void smallestPermute(int n)
      {
          char res[] = new char[n + 1];
        
          // when n is even
          if (n % 2 == 0) {
              for (int i = 0; i < n; i++) 
              {
                  if (i % 2 == 0)
                      res[i] = (char)(48 + i + 2);
                  else
                      res[i] = (char)(48 + i);
              }
          }
        
          // when n is odd
          else 
          {
              for (int i = 0; i < n - 2; i++) 
              {
                  if (i % 2 == 0)
                      res[i] = (char)(48 + i + 2);
                  else
                      res[i] = (char)(48 + i);
              }
              // handling last 3 digit
              res[n - 1] = (char)(48 + n - 2);
              res[n - 2] = (char)(48 + n);
              res[n - 3] = (char)(48 + n - 1);
          }
        
          // add EOL and print result
          res[n] = '\0';
        
          for (int i = 0; i < n ; i++)
          {
              System.out.print(res[i]);
          }
      }
        
      // Driver Code
      public static void main(String []args)
      {
          int n = 7;
        
          smallestPermute(n);
      }
      }
        
      // This code is contributed by ANKITRAI1


      Python 3
      # Python 3 program to find the 
      # smallest permutaion
        
      # Function to print the smallest
      # permutation
      def smallestPermute( n):
        
          res = [""] * (n + 1)
            
          # when n is even
          if (n % 2 == 0) :
              for i in range(n):
                  if (i % 2 == 0):
                      res[i] = chr(48 + i + 2)
                  else:
                      res[i] = chr(48 + i)
        
          # when n is odd
          else :
              for i in range(n - 2 ):
                  if (i % 2 == 0):
                      res[i] = chr(48 + i + 2)
                  else:
                      res[i] = chr(48 + i)
                
              # handling last 3 digit
              res[n - 1] = chr(48 + n - 2)
              res[n - 2] = chr(48 + n)
              res[n - 3] = chr(48 + n - 1)
        
          # add EOL and print result
          res = ''.join(res)
          return res
        
      # Driver Code
      if __name__ == "__main__":
            
          n = 7
          print(smallestPermute(n))
        
      # This code is contributed by ita_c


      C#
      // C# program to find the smallest 
      // permutaion
      using System;
      class GFG
      {
        
      // Function to print the smallest 
      // permutation
      static void smallestPermute(int n)
      {
          char[] res = new char[n + 1];
        
          // when n is even
          if (n % 2 == 0) 
          {
              for (int i = 0; i < n; i++) 
              {
                  if (i % 2 == 0)
                      res[i] = (char)(48 + i + 2);
                  else
                      res[i] = (char)(48 + i);
              }
          }
        
          // when n is odd
          else
          {
              for (int i = 0; i < n - 2; i++) 
              {
                  if (i % 2 == 0)
                      res[i] = (char)(48 + i + 2);
                  else
                      res[i] = (char)(48 + i);
              }
              // handling last 3 digit
              res[n - 1] = (char)(48 + n - 2);
              res[n - 2] = (char)(48 + n);
              res[n - 3] = (char)(48 + n - 1);
          }
        
          // add EOL and print result
          res[n] = '\0';
        
          for (int i = 0; i < n ; i++)
          {
              Console.Write(res[i]);
          }
      }
        
      // Driver Code
      public static void Main()
      {
          int n = 7;
        
          smallestPermute(n);
      }
      }
        
      // This code is contributed 
      // by Akanksha Rai


      PHP


      输出:
      2143675