📜  将给定数字的位数旋转K

📅  最后修改于: 2021-05-04 23:48:33             🧑  作者: Mango

给定两个整数NK ,任务是将N的数字旋转K。如果K是一个正整数,则向左旋转其数字。否则,右旋转其数字。

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个变量,例如X ,以将数字的计数存储在N中
  • 更新K =(K + X)%X,以将其减少到左旋转的情况。
  • 除去N的前K位和所有拆下的数字附加到的N的位数的权利。
  • 最后,打印N的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the count of
// digits in N
int numberOfDigit(int N)
{
 
    // Stores count of
    // digits in N
    int digit = 0;
 
    // Calculate the count
    // of digits in N
    while (N > 0) {
 
        // Update digit
        digit++;
 
        // Update N
        N /= 10;
    }
    return digit;
}
 
// Function to rotate the digits of N by K
void rotateNumberByK(int N, int K)
{
 
    // Stores count of digits in N
    int X = numberOfDigit(N);
 
    // Update K so that only need to
    // handle left rotation
    K = ((K % X) + X) % X;
 
    // Stores first K digits of N
    int left_no = N / (int)(pow(10, X - K));
 
    // Remove first K digits of N
    N = N % (int)(pow(10, X - K));
 
    // Stores count of digits in left_no
    int left_digit = numberOfDigit(left_no);
 
    // Append left_no to the right of
    // digits of N
    N = (N * (int)(pow(10, left_digit))) + left_no;
    cout << N;
}
 
// Driver code
int main()
{
    int N = 12345, K = 7;
 
    // Function Call
    rotateNumberByK(N, K);
    return 0;
}
 
// The code is contributed by Dharanendra L V


Java
// Java program to implement
// the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find the count of
    // digits in N
    static int numberOfDigit(int N)
    {
 
        // Stores count of
        // digits in N
        int digit = 0;
 
        // Calculate the count
        // of digits in N
        while (N > 0) {
 
            // Update digit
            digit++;
 
            // Update N
            N /= 10;
        }
        return digit;
    }
 
    // Function to rotate the digits of N by K
    static void rotateNumberByK(int N, int K)
    {
 
        // Stores count of digits in N
        int X = numberOfDigit(N);
 
        // Update K so that only need to
        // handle left rotation
        K = ((K % X) + X) % X;
 
        // Stores first K digits of N
        int left_no = N / (int)(Math.pow(10,
                                         X - K));
 
        // Remove first K digits of N
        N = N % (int)(Math.pow(10, X - K));
 
        // Stores count of digits in left_no
        int left_digit = numberOfDigit(left_no);
 
        // Append left_no to the right of
        // digits of N
        N = (N * (int)(Math.pow(10, left_digit)))
            + left_no;
 
        System.out.println(N);
    }
 
    // Driver Code
    public static void main(String args[])
    {
 
        int N = 12345, K = 7;
 
        // Function Call
        rotateNumberByK(N, K);
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to find the count of
# digits in N
def numberOfDigit(N):
 
    # Stores count of
    # digits in N
    digit = 0
 
    # Calculate the count
    # of digits in N
    while (N > 0):
 
        # Update digit
        digit += 1
 
        # Update N
        N //= 10
    return digit
 
# Function to rotate the digits of N by K
def rotateNumberByK(N, K):
 
    # Stores count of digits in N
    X = numberOfDigit(N)
 
    # Update K so that only need to
    # handle left rotation
    K = ((K % X) + X) % X
 
    # Stores first K digits of N
    left_no = N // pow(10, X - K)
 
    # Remove first K digits of N
    N = N % pow(10, X - K)
 
    # Stores count of digits in left_no
    left_digit = numberOfDigit(left_no)
 
    # Append left_no to the right of
    # digits of N
    N = N * pow(10, left_digit) + left_no
    print(N)
 
# Driver Code
if __name__ == '__main__':
    N, K = 12345, 7
 
    # Function Call
    rotateNumberByK(N, K)
 
    # This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
    // Function to find the count of
    // digits in N
    static int numberOfDigit(int N)
    {
 
        // Stores count of
        // digits in N
        int digit = 0;
 
        // Calculate the count
        // of digits in N
        while (N > 0)
        {
 
            // Update digit
            digit++;
 
            // Update N
            N /= 10;
        }
        return digit;
    }
 
    // Function to rotate the digits of N by K
    static void rotateNumberByK(int N, int K)
    {
 
        // Stores count of digits in N
        int X = numberOfDigit(N);
 
        // Update K so that only need to
        // handle left rotation
        K = ((K % X) + X) % X;
 
        // Stores first K digits of N
        int left_no = N / (int)(Math.Pow(10,
                                         X - K));
 
        // Remove first K digits of N
        N = N % (int)(Math.Pow(10, X - K));
 
        // Stores count of digits in left_no
        int left_digit = numberOfDigit(left_no);
 
        // Append left_no to the right of
        // digits of N
        N = (N * (int)(Math.Pow(10, left_digit)))
            + left_no;
 
        Console.WriteLine(N);
    }
 
    // Driver Code
    public static void Main(string []args)
    {
 
        int N = 12345, K = 7;
 
        // Function Call
        rotateNumberByK(N, K);
    }
}
 
// This code is contributed by AnkThon


输出:
34512

时间复杂度: O(log 10 N)
辅助空间: O(1)