📌  相关文章
📜  检查给定数字的所有旋转是否大于或等于给定数字

📅  最后修改于: 2021-04-24 19:06:25             🧑  作者: Mango

给定整数x ,任务是查找元素上的每个k周期移位是否产生大于或等于相同元素的数字。
整数x的k循环移位是删除x的最后k个数字并将其插入其开头的函数。
例如,对于k = 1 ,k循环移位123312 ,对于k = 2 ,k循环移位为231 。如果满足给定条件,则打印是”,否则打印“否”
例子:

方法:只需找到该数字的所有可能的k个循环移位,然后检查所有数字是否大于给定数字。
下面是上述方法的实现:

C++
// CPP implementation of the approach
#include
using namespace std;
 
void CheckKCycles(int n, string s)
{
    bool ff = true;
    int x = 0;
    for (int i = 1; i < n; i++)
    {
 
        // Splitting the number at index i
        // and adding to the front
        x = (s.substr(i) + s.substr(0, i)).length();
 
        // Checking if the value is greater than
        // or equal to the given value
        if (x >= s.length())
        {
            continue;
        }
        ff = false;
        break;
    }
    if (ff)
    {
        cout << ("Yes");
    }
    else
    {
        cout << ("No");
    }
}
 
// Driver code
int main()
{
    int n = 3;
    string s = "123";
    CheckKCycles(n, s);
    return 0;
}
 
/* This code contributed by Rajput-Ji */


Java
// Java implementation of the approach
class GFG
{
 
    static void CheckKCycles(int n, String s)
    {
        boolean ff = true;
        int x = 0;
        for (int i = 1; i < n; i++)
        {
 
            // Splitting the number at index i
            // and adding to the front
            x = (s.substring(i) + s.substring(0, i)).length();
 
            // Checking if the value is greater than
            // or equal to the given value
            if (x >= s.length())
            {
                continue;
            }
            ff = false;
            break;
        }
        if (ff)
        {
            System.out.println("Yes");
        }
        else
        {
            System.out.println("No");
        }
 
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 3;
        String s = "123";
        CheckKCycles(n, s);
    }
}
 
/* This code contributed by PrinciRaj1992 */


Python
# Python3 implementation of the approach
def CheckKCycles(n, s):
    ff = True
    for i in range(1, n):
 
        # Splitting the number at index i
        # and adding to the front
        x = int(s[i:] + s[0:i])
 
        # Checking if the value is greater than
        # or equal to the given value
        if (x >= int(s)):
            continue
        ff = False
        break
    if (ff):
        print("Yes")
    else:
        print("No")
 
n = 3
s = "123"
CheckKCycles(n, s)


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    static void CheckKCycles(int n, String s)
    {
        bool ff = true;
        int x = 0;
        for (int i = 1; i < n; i++)
        {
 
            // Splitting the number at index i
            // and adding to the front
            x = (s.Substring(i) + s.Substring(0, i)).Length;
 
            // Checking if the value is greater than
            // or equal to the given value
            if (x >= s.Length)
            {
                continue;
            }
            ff = false;
            break;
        }
        if (ff)
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
 
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 3;
        String s = "123";
        CheckKCycles(n, s);
    }
}
 
// This code has been contributed by 29AjayKumar


PHP
= strlen($s))
        {
            continue;
        }
        $ff = false;
        break;
    }
    if ($ff)
    {
        print("Yes");
    }
    else
    {
        print("No");
    }
}
 
// Driver code
$n = 3;
$s = "123";
CheckKCycles($n, $s);
 
// This code contributed by mits
?>


Javascript


输出:
Yes