📜  以特定方式填充从1到n的所有数字的两个实例

📅  最后修改于: 2021-05-24 21:04:25             🧑  作者: Mango

给定一个数字n,创建一个大小为2n的数组,以使该数组包含从1到n的每个数字的2个实例,并且在数量为i的两个实例之间的元素数等于i。如果无法进行这种配置,请打印相同的内容。
例子:

Input: n = 3
Output: res[] = {3, 1, 2, 1, 3, 2}

Input: n = 2
Output: Not Possible

Input: n = 4
Output: res[] = {4, 1, 3, 1, 2, 4, 3, 2}

强烈建议您最小化浏览器,然后自己尝试。
一种解决方案是回溯。这个想法很简单,我们将n的两个实例放在一个位置,然后递归n-1。如果重复成功,则返回true,否则我们回溯并尝试将n放置在其他位置。以下是该想法的实现。

C++
// A backtracking based C++ Program to fill
// two instances of all numbers from 1 to n
// in a specific way
#include 
using namespace std;
 
// A recursive utility function to fill
// two instances of numbers from 1 to n
// in res[0..2n-1]. 'curr' is current value of n.
bool fillUtil(int res[], int curr, int n)
{
    // If current number becomes 0,
    // then all numbers are filled
    if (curr == 0)
    return true;
 
    // Try placing two instances of 'curr' at
    // all possible locations till solution is found
    int i;
    for (i = 0; i < 2 * n - curr - 1; i++)
    {
        // Two 'curr' should be placed at
        // 'curr+1' distance
        if (res[i] == 0 && res[i + curr + 1] == 0)
        {
             
            // Plave two instances of 'curr'
            res[i] = res[i + curr + 1] = curr;
     
            // Recur to check if the above placement
            // leads to a solution
            if (fillUtil(res, curr - 1, n))
                return true;
     
            // If solution is not possible,
            // then backtrack
            res[i] = res[i + curr + 1] = 0;
        }
    }
    return false;
}
 
// This function prints the result for
// input number 'n' using fillUtil()
void fill(int n)
{
    // Create an array of size 2n and
    // initialize all elements in it as 0
    int res[2 * n], i;
    for (i = 0; i < 2 * n; i++)
    res[i] = 0;
 
    // If solution is possible,
    // then print it.
    if (fillUtil(res, n, n))
    {
        for (i = 0; i < 2 * n; i++)
        cout << res[i] << " ";
    }
    else
        cout << "Not Possible";
}
 
// Driver Code
int main()
{
    fill(7);
    return 0;
}
 
// This code is contributed
// by SHUBHAMSINGH8410


C
// A backtracking based C Program to fill two instances of all numbers
// from 1 to n in a specific way
#include 
#include 
 
// A recursive utility function to fill two instances of numbers from
// 1 to n in res[0..2n-1].  'curr' is current value of n.
bool fillUtil(int res[], int curr, int n)
{
     // If current number becomes 0, then all numbers are filled
     if (curr == 0) return true;
 
     // Try placing two instances of 'curr' at all possible locations
     // till solution is found
     int i;
     for (i=0; i<2*n-curr-1; i++)
     {
        // Two 'curr' should be placed at 'curr+1' distance
        if (res[i] == 0 && res[i + curr + 1] == 0)
        {
           // Plave two instances of 'curr'
           res[i] = res[i + curr + 1] = curr;
 
           // Recur to check if the above placement leads to a solution
           if (fillUtil(res, curr-1, n))
               return true;
 
           // If solution is not possible, then backtrack
           res[i] = res[i + curr + 1] = 0;
        }
     }
     return false;
}
 
// This function prints the result for input number 'n' using fillUtil()
void fill(int n)
{
    // Create an array of size 2n and initialize all elements in it as 0
    int res[2*n], i;
    for (i=0; i<2*n; i++)
       res[i] = 0;
 
    // If solution is possible, then print it.
    if (fillUtil(res, n, n))
    {
        for (i=0; i<2*n; i++)
           printf("%d ", res[i]);
    }
    else
        puts("Not Possible");
}
 
// Driver program
int main()
{
  fill(7);
  return 0;
}


Java
// A backtracking based C++ Program to fill
// two instances of all numbers from 1 to n
// in a specific way
import java.io.*;
 
class GFG
{
     
// A recursive utility function to fill
// two instances of numbers from 1 to n
// in res[0..2n-1]. 'curr' is current value of n.
static boolean fillUtil(int res[], int curr, int n)
{
    // If current number becomes 0,
    // then all numbers are filled
    if (curr == 0)
    return true;
 
    // Try placing two instances of 'curr' at
    // all possible locations till solution is found
    int i;
    for (i = 0; i < 2 * n - curr - 1; i++)
    {
        // Two 'curr' should be placed at
        // 'curr+1' distance
        if (res[i] == 0 && res[i + curr + 1] == 0)
        {
             
            // Plave two instances of 'curr'
            res[i] = res[i + curr + 1] = curr;
     
            // Recur to check if the above placement
            // leads to a solution
            if (fillUtil(res, curr - 1, n))
                return true;
     
            // If solution is not possible,
            // then backtrack
            res[i] = res[i + curr + 1] = 0;
        }
    }
    return false;
}
 
// This function prints the result for
// input number 'n' using fillUtil()
static void fill(int n)
{
    // Create an array of size 2n and
    // initialize all elements in it as 0
    int res[] = new int[2 * n];
    int i;
    for (i = 0; i < 2 * n; i++)
    res[i] = 0;
 
    // If solution is possible,
    // then print it.
    if (fillUtil(res, n, n))
    {
        for (i = 0; i < 2 * n; i++)
            System.out.print(res[i] + " ");
    }
    else
        System.out.print("Not Possible");
}
 
// Driver Code
public static void main (String[] args)
{
    fill(7);
}
}
 
// This code is contributed by ajit


Python3
# A backtracking based Python3 Program
# to fill two instances of all numbers
# from 1 to n in a specific way
def fillUtil(res, curr, n):
     
    # A recursive utility function to fill
    # two instances of numbers from 1 to n
    # in res[0..2n-1]. 'curr' is current value of n.
 
    # If current number becomes 0,
    # then all numbers are filled
    if curr == 0:
        return True
 
    # Try placing two instances of 'curr' at all
    # possible locations till solution is found
    for i in range(2 * n - curr - 1):
 
        # Two 'curr' should be placed
        # at 'curr+1' distance
        if res[i] == 0 and res[i + curr + 1] == 0:
 
            # Place two instances of 'curr'
            res[i] = res[i + curr + 1] = curr
 
            # Recur to check if the above
            # placement leads to a solution
            if fillUtil(res, curr - 1, n):
                return True
 
            # If solution is not possible,
            # then backtrack
            res[i] = 0
            res[i + curr + 1] = 0
 
    return False
 
def fill(n):
     
    # This function prints the result
    # for input number 'n' using fillUtil()
 
    # Create an array of size 2n and
    # initialize all elements in it as 0
    res = [0] * (2 * n)
 
    # If solution is possible, then print it.
    if fillUtil(res, n, n):
        for i in range(2 * n):
            print(res[i], end = ' ')
        print()
    else:
        print("Not Possible")
 
# Driver Code
if __name__ == '__main__':
    fill(7)
 
# This code is contributed by vibhu4agarwal


C#
// A backtracking based C# Program to fill
// two instances of all numbers from 1 to n
// in a specific way
using System;
 
class GFG
{
     
// A recursive utility function to fill
// two instances of numbers from 1 to n
// in res[0..2n-1]. 'curr' is current value of n.
static bool fillUtil(int []res, int curr, int n)
{
    // If current number becomes 0,
    // then all numbers are filled
    if (curr == 0)
    return true;
 
    // Try placing two instances of 'curr' at
    // all possible locations till solution is found
    int i;
    for (i = 0; i < 2 * n - curr - 1; i++)
    {
        // Two 'curr' should be placed at
        // 'curr+1' distance
        if (res[i] == 0 && res[i + curr + 1] == 0)
        {
             
            // Plave two instances of 'curr'
            res[i] = res[i + curr + 1] = curr;
     
            // Recur to check if the above placement
            // leads to a solution
            if (fillUtil(res, curr - 1, n))
                return true;
     
            // If solution is not possible,
            // then backtrack
            res[i] = res[i + curr + 1] = 0;
        }
    }
    return false;
}
 
// This function prints the result for
// input number 'n' using fillUtil()
static void fill(int n)
{
    // Create an array of size 2n and
    // initialize all elements in it as 0
    int []res=new int[2 * n];
    int i;
    for (i = 0; i < (2 * n); i++)
    res[i] = 0;
 
    // If solution is possible,
    // then print it.
    if (fillUtil(res, n, n))
    {
        for (i = 0; i < 2 * n; i++)
        Console.Write (res[i] + " ");
    }
    else
        Console.Write ("Not Possible");
}
 
// Driver Code
static public void Main ()
{
    fill(7);
}
}
 
// This code is contributed by ajit


Javascript


输出:

7 3 6 2 5 3 2 4 7 6 5 1 4 1