给定数字N,任务是检查数字频率的异或值是否为零。
例子:
Input: N = 122233
Output: Yes
Frequencies of 1, 2 and 3 are 1, 3, 2 respectively.
And Xor of 1, 3 and 2 is 0.
Input: N = 123
Output: No
方法:计算所有数字的频率,然后遍历所有频率,如果答案为零,则对它们进行异或运算,然后打印“是”,否则。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
bool check(int s)
{
// creating a frequency array
int freq[10] = {0},r;
while(s != 0)
{
// Finding the last digit of the number
r = s % 10;
// Dividing the number by 10 to
// eliminate last digit
s = int(s / 10);
// counting frequency of each digit
freq[r] += 1;
}
int xor__ = 0;
// checking if the xor of all frequency is zero or not
for (int i=0;i<10;i++)
{
xor__ = xor__ ^ freq[i];
if(xor__ == 0)
return true;
else
return false;
}
}
// Driver function
int main()
{
int s = 122233;
if(check(s))
cout<<"Yes"<
Java
// Java implementation of the above approach
class GFG
{
static boolean check(int s)
{
// creating a frequency array
int[] freq = new int[10];
int r,i;
for(i=0;i<10;i++)
{
freq[i]= 0;
}
while(s != 0)
{
// Finding the last digit of the number
r = s % 10;
// Dividing the number by 10 to
// eliminate last digit
s = (int)(s / 10);
// counting frequency of each digit
freq[r] += 1;
}
int xor__ = 0;
// checking if the xor of all frequency is zero or not
for ( i=0;i<10;i++)
{
xor__ = xor__ ^ freq[i];
if(xor__ == 0)
return true;
else
return false;
}
return true;
}
// Driver function
public static void main(String[] args) {
int s = 122233;
if(check(s))
System.out.println("Yes\n");
else
System.out.println("No\n");
}
// This code is contributed by
// Rajput-Ji
}
Python3
# Python implementation of the above approach
def check(s):
# creating a frequency array
freq =[0]*10
while(s != 0):
# Finding the last digit of the number
r = s % 10
# Dividing the number by 10 to
# eliminate last digit
s = s//10
# counting frequency of each digit
freq[r]+= 1
xor = 0
# checking if the xor of all frequency is zero or not
for i in range(10):
xor = xor ^ freq[i]
if(xor == 0):
return True
else:
return False
s = 122233
if(check(s)):
print("Yes")
else:
print("No")
C#
// C# implementation of the above approach
using System;
class GFG
{
static bool check(int s)
{
// creating a frequency array
int[] freq = new int[10];
int r, i;
for(i = 0; i < 10; i++)
{
freq[i]= 0;
}
while(s != 0)
{
// Finding the last digit of the number
r = s % 10;
// Dividing the number by 10 to
// eliminate last digit
s = (int)(s / 10);
// counting frequency of each digit
freq[r] += 1;
}
int xor__ = 0;
// checking if the xor of all frequency is zero or not
for ( i = 0; i < 10; i++)
{
xor__ = xor__ ^ freq[i];
if(xor__ == 0)
return true;
else
return false;
}
return true;
}
// Driver code
public static void Main()
{
int s = 122233;
if(check(s))
Console.Write("Yes\n");
else
Console.Write("No\n");
}
}
// This code is contributed by Ita_c.
PHP
输出:
Yes