📌  相关文章
📜  生成数字的所有循环排列

📅  最后修改于: 2021-04-29 08:26:20             🧑  作者: Mango

给定数字N,我们的任务是生成数字的所有可能的循环排列。

循环置换将集合的所有元素移动固定的偏移量。对于带有元素的集合a_0a_1 ,…, a_n ,向左移动一个位置的循环置换将产生a_1 ,…, a_na_0 ,并且在右侧对一个位置进行循环排列会产生a_na_0a_1 ,…。

例子:

Input :  123
Output : 123
         312
         231

Input :  5674
Output : 5674
         4567
         7456
         6745

想法是使用以下公式生成数字的下一个排列。

rem = num % 10;
    div = num / 10;
    num = (pow(10, n - 1)) * rem + div;

在重复上述步骤时,如果我们返回原始编号,我们将停止并返回。

C++
// Program to generate all cyclic permutations
// of number
#include 
using namespace std;
  
// Function to count the total number of digits 
// in a number.
int countdigits(int N)
{
    int count = 0;
    while (N) {
        count++;
        N = N / 10;
    }
    return count;
}
  
// Function to generate all cyclic permutations
// of a number
void cyclic(int N)
{
    int num = N;
    int n = countdigits(N);
  
    while (1) {
        cout << num << endl;
  
        // Following three lines generates a
        // circular pirmutation of a number.
        int rem = num % 10;
        int div = num / 10;
        num = (pow(10, n - 1)) * rem + div;
  
        // If all the permutations are checked
        // and we obtain original number exit
        // from loop.
        if (num == N) 
            break;        
    }
}
  
// Driver Program
int main()
{
    int N = 5674;
    cyclic(N);
    return 0;
}


Java
// Java Program to generate all 
// cyclic permutations of number
class GFG
{
  
    // Function to count the total number
    // of digits in a number.
    static int countdigits(int N)
    {
        int count = 0;
        while (N>0) {
            count++;
            N = N / 10;
        }
        return count;
    }
  
    // Function to generate all cyclic
    // permutations of a number
    static void cyclic(int N)
    {
        int num = N;
        int n = countdigits(N);
  
        while (true) {
            System.out.println(num); 
  
            // Following three lines generates a
            // circular pirmutation of a number.
            int rem = num % 10;
            int dev = num / 10;
            num = (int)((Math.pow(10, n - 1)) *
                                rem + dev);
  
            // If all the permutations are 
            // checked and we obtain original
            // number exit from loop.
            if (num == N) 
                break; 
        }
    }
  
    // Driver Program
    public static void main (String[] args) {
    int N = 5674;
    cyclic(N);
    }
}
  
/* This code is contributed by Mr. Somesh Awasthi */


Python3
# Python3 Program to 
# generate all cyclic
# permutations of number
import math
  
# Function to count the 
# total number of digits
# in a number.
def countdigits(N):
    count = 0;
    while (N):
        count = count + 1;
        N = int(math.floor(N / 10));
    return count;
      
# Function to generate 
# all cyclic permutations
# of a number
def cyclic(N):
    num = N;
    n = countdigits(N);
    while (1):
        print(int(num));
          
        # Following three lines 
        # generates a circular 
        # permutation of a number.
        rem = num % 10;
        div = math.floor(num / 10);
        num = ((math.pow(10, n - 1)) * 
                           rem + div);
          
        # If all the permutations 
        # are checked and we obtain 
        # original number exit from loop.
        if (num == N):
            break; 
              
# Driver Code
N = 5674;
cyclic(N);
  
# This code is contributed by mits


C#
// C# Program to generate all 
// cyclic permutations of number
using System;
  
class GFG
{
    // Function to count the total number
    // of digits in a number.
    static int countdigits(int N)
    {
        int count = 0;
        while (N > 0) {
            count++;
            N = N / 10;
        }
        return count;
    }
  
    // Function to generate all cyclic
    // permutations of a number
    static void cyclic(int N)
    {
        int num = N;
        int n = countdigits(N);
  
        while (true) {
            Console.WriteLine(num); 
  
            // Following three lines generates a
            // circular permutation of a number.
            int rem = num % 10;
            int dev = num / 10;
            num = (int)((Math.Pow(10, n - 1)) *
                                    rem + dev);
  
            // If all the permutations are 
            // checked and we obtain original
            // number exit from loop.
            if (num == N) 
                break; 
        }
    }
  
    // Driver Program
    public static void Main () 
    {
      int N = 5674;
      cyclic(N);
    }
}
  
// This code is contributed by nitin mittal


PHP


输出:

5674
 4567
 7456
 6745