📌  相关文章
📜  旋转给定数字的位数可能达到的最大值

📅  最后修改于: 2021-04-17 18:25:08             🧑  作者: Mango

给定正整数N ,任务是在整数N的数字的所有旋转中找到最大值。

例子:

方法:这个想法是找到数字N的所有旋转,并在所有生成的数字中打印最大值。请按照以下步骤解决问题:

  • 计算数字N中存在的位数,即对10 N的上限。
  • 使用N的值初始化变量ans ,以存储生成的结果最大数量。
  • 遍历范围 [1,记录10 (N)– 1],然后执行以下步骤:
    • 下一轮更新N的值。
    • 现在,如果生成的下一个旋转超过ans ,则使用N的旋转值更新ans
  • 完成上述步骤后,将ans的值打印为所需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum value
// possible by rotations of digits of N
void findLargestRotation(int num)
{
    // Store the required result
    int ans = num;
 
    // Store the number of digits
    int len = floor(log10(num) + 1);
 
    int x = pow(10, len - 1);
 
    // Iterate over the range[1, len-1]
    for (int i = 1; i < len; i++) {
 
        // Store the unit's digit
        int lastDigit = num % 10;
 
        // Store the remaining number
        num = num / 10;
 
        // Find the next rotation
        num += (lastDigit * x);
 
        // If the current rotation is
        // greater than the overall
        // answer, then update answer
        if (num > ans) {
            ans = num;
        }
    }
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    int N = 657;
    findLargestRotation(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find the maximum value
// possible by rotations of digits of N
static void findLargestRotation(int num)
{
   
    // Store the required result
    int ans = num;
 
    // Store the number of digits
    int len = (int)Math.floor(((int)Math.log10(num)) + 1);
    int x = (int)Math.pow(10, len - 1);
 
    // Iterate over the range[1, len-1]
    for (int i = 1; i < len; i++) {
 
        // Store the unit's digit
        int lastDigit = num % 10;
 
        // Store the remaining number
        num = num / 10;
 
        // Find the next rotation
        num += (lastDigit * x);
 
        // If the current rotation is
        // greater than the overall
        // answer, then update answer
        if (num > ans) {
            ans = num;
        }
    }
 
    // Print the result
    System.out.print(ans);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 657;
    findLargestRotation(N);
}
}
 
// This code is contributed by sanjoy_62.


Python3
# Python program for the above approach
 
# Function to find the maximum value
# possible by rotations of digits of N
def findLargestRotation(num):
   
    # Store the required result
    ans = num
     
    # Store the number of digits
    length = len(str(num))
    x = 10**(length - 1)
     
    # Iterate over the range[1, len-1]
    for i in range(1, length):
       
        # Store the unit's digit
        lastDigit = num % 10
         
        # Store the remaining number
        num = num // 10
         
        # Find the next rotation
        num += (lastDigit * x)
         
        # If the current rotation is
        # greater than the overall
        # answer, then update answer
        if (num > ans):
            ans = num
             
    # Print the result
    print(ans)
 
# Driver Code
N = 657
findLargestRotation(N)
 
# This code is contributed by rohitsingh07052.


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find the maximum value
// possible by rotations of digits of N
static void findLargestRotation(int num)
{
   
    // Store the required result
    int ans = num;
 
    // Store the number of digits
    double lg = (double)(Math.Log10(num) + 1);
    int len = (int)(Math.Floor(lg));
    int x = (int)Math.Pow(10, len - 1);
 
    // Iterate over the range[1, len-1]
    for (int i = 1; i < len; i++) {
 
        // Store the unit's digit
        int lastDigit = num % 10;
 
        // Store the remaining number
        num = num / 10;
 
        // Find the next rotation
        num += (lastDigit * x);
 
        // If the current rotation is
        // greater than the overall
        // answer, then update answer
        if (num > ans) {
            ans = num;
        }
    }
 
    // Print the result
    Console.Write(ans);
}
 
// Driver Code
public static void Main(string[] args)
{
    int N = 657;
    findLargestRotation(N);
}
}
 
// This code is contributed by souravghosh0416,


Javascript


输出:
765

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