给定整数n ,任务是生成所有可能的左移数。左移数字是当该数字的所有数字都向左移动一个位置,而第一个位置的数字移动到最后一个数字时生成的数字。
例子:
Input: n = 123
Output: 231 312
Input: n = 1445
Output: 4451 4514 5144
方法:
- 假设n = 123 。
- 将n乘以10,即n = n * 10 = 1230 。
- 将第一个数字加到结果数字上,即1230 +1 = 1231 。
- 从结果数中减去(第一个数字)* 10 k ,其中k是原始数字的位数(在这种情况下,k = 3)。
- 1231 – 1000 = 231是原始编号的左移编号。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count of digits of n
int numberOfDigits(int n)
{
int cnt = 0;
while (n > 0) {
cnt++;
n /= 10;
}
return cnt;
}
// Function to print the left shift numbers
void cal(int num)
{
int digits = numberOfDigits(num);
int powTen = pow(10, digits - 1);
for (int i = 0; i < digits - 1; i++) {
int firstDigit = num / powTen;
// Formula to calculate left shift
// from previous number
int left
= ((num * 10) + firstDigit)
- (firstDigit * powTen * 10);
cout << left << " ";
// Update the original number
num = left;
}
}
// Driver Code
int main()
{
int num = 1445;
cal(num);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the count of digits of n
static int numberOfDigits(int n)
{
int cnt = 0;
while (n > 0)
{
cnt++;
n /= 10;
}
return cnt;
}
// Function to print the left shift numbers
static void cal(int num)
{
int digits = numberOfDigits(num);
int powTen = (int) Math.pow(10, digits - 1);
for (int i = 0; i < digits - 1; i++)
{
int firstDigit = num / powTen;
// Formula to calculate left shift
// from previous number
int left = ((num * 10) + firstDigit) -
(firstDigit * powTen * 10);
System.out.print(left + " ");
// Update the original number
num = left;
}
}
// Driver Code
public static void main(String[] args)
{
int num = 1445;
cal(num);
}
}
// This code is contributed by
// PrinciRaj1992
Python3
# Python3 implementation of the approach
# function to return the count of digit of n
def numberofDigits(n):
cnt = 0
while n > 0:
cnt += 1
n //= 10
return cnt
# function to print the left shift numbers
def cal(num):
digit = numberofDigits(num)
powTen = pow(10, digit - 1)
for i in range(digit - 1):
firstDigit = num // powTen
# formula to calculate left shift
# from previous number
left = (num * 10 + firstDigit -
(firstDigit * powTen * 10))
print(left, end = " ")
# Update the original number
num = left
# Driver code
num = 1445
cal(num)
# This code is contributed
# by Mohit Kumar
C#
// C# implementation of the approach
using System;
public class GFG{
// Function to return the count of digits of n
static int numberOfDigits(int n)
{
int cnt = 0;
while (n > 0) {
cnt++;
n /= 10;
}
return cnt;
}
// Function to print the left shift numbers
static void cal(int num)
{
int digits = numberOfDigits(num);
int powTen = (int)Math.Pow(10, digits - 1);
for (int i = 0; i < digits - 1; i++) {
int firstDigit = num / powTen;
// Formula to calculate left shift
// from previous number
int left
= ((num * 10) + firstDigit)
- (firstDigit * powTen * 10);
Console.Write(left + " ");
// Update the original number
num = left;
}
}
// Driver Code
static public void Main (){
int num = 1445;
cal(num);
}
}
// This code is contributed by akt_mit....
PHP
0)
{
$cnt++;
$n = floor($n / 10);
}
return $cnt;
}
// Function to print the left shift numbers
function cal($num)
{
$digits = numberOfDigits($num);
$powTen = pow(10, $digits - 1);
for ($i = 0; $i < $digits - 1; $i++)
{
$firstDigit = floor($num / $powTen);
// Formula to calculate left shift
// from previous number
$left
= (($num * 10) + $firstDigit) -
($firstDigit * $powTen * 10);
echo $left, " ";
// Update the original number
$num = $left;
}
}
// Driver Code
$num = 1445;
cal($num);
// This code is contributed by Ryuga
?>
输出:
4451 4514 5144