📜  Java程序计算可被10整除的旋转

📅  最后修改于: 2022-05-13 01:54:33.574000             🧑  作者: Mango

Java程序计算可被10整除的旋转

给定一个数字N ,任务是计算给定数字的所有可被 10 整除的旋转。
例子:

朴素方法:这个问题的朴素方法是形成所有可能的旋转。已知对于大小为K的数,该数N的可能旋转次数为K。因此,找到所有的旋转,对于每一个旋转,检查数字是否能被 10 整除。这种方法的时间复杂度是二次的。
有效方法:有效方法背后的概念是,为了检查一个数字是否能被 10 整除,我们只需检查最后一位是否为 0。因此,这个想法是简单地迭代给定的数字并找到0 的计数。如果 0 的计数是F ,那么显然, K次旋转中的F在给定数字N的末尾将具有 0。
下面是上述方法的实现:

Java
// Java implementation to find the
// count of rotations which are
// divisible by 10
  
class GFG {
  
    // Function to return the count
    // of all rotations which are
    // divisible by 10.
    static int countRotation(int n)
    {
        int count = 0;
  
        // Loop to iterate through the
        // number
        do {
            int digit = n % 10;
  
            // If the last digit is 0,
            // then increment the count
            if (digit == 0)
                count++;
            n = n / 10;
        } while (n != 0);
  
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 10203;
  
        System.out.println(countRotation(n));
    }
}


输出:
2

时间复杂度: O(N) ,其中 N 是数字的长度。

有关详细信息,请参阅有关可被 10 整除的计数旋转的完整文章!