给定一个整数x ,任务是找出元素上的每个 k 循环移位是否产生大于或等于相同元素的数字。
整数x 的k 循环移位是一个函数,它删除x的最后k位并将它们插入到其开头。
例如, 123的 k 循环移位对于k=1是312 ,对于k=2 是231 。如果满足给定条件则打印Yes否则打印No 。
例子:
Input: x = 123
Output : Yes
The k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2.
Both 312 and 231 are greater than 123.
Input: 2214
Output: No
The k-cyclic shift of 2214 when k=2 is 1422 which is smaller than 2214
方法:简单地找出数字的所有可能的 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