📌  相关文章
📜  原始索引处没有数字的字典序最小排列

📅  最后修改于: 2021-10-26 05:41:29             🧑  作者: 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 是可能的最小排列。

算法

If N is even:place all even digits (upto N) in increasing order at odd index.place all odd digits in increasing order at even index.elseplace all even digits (upto N-3) in increasing order at odd index.place all odd digits (upto N-4) in increasing order at even index.Place N at (N-1)th place, N-1 at (N-2)th and N-2 at Nth place.

下面是上述方法的实现:

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


Javascript


输出:
2143675

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程