给定两个整数N和B ,任务是检查N是基数B中的数字平衡数字。
Digitally balanced numbers in the base B are those numbers that have the same number of (0, 1, 2 ….B-1) digits in base B.
例子:
Input: N = 9, B = 2
Output: Yes
Equivalent binary number of 9 is 1001,
which has equal number of 0’s as 1’s = 2
Input: N = 5, N = 2
Output: No
Equivalent binary number of 5 is 101.
方法:想法是遍历基数B中的数字并将数字的频率存储在哈希图中。最后,在哈希图中进行迭代,并检查哈希图中每个数字的频率是否相同,然后将该数字称为基数B中的数字平衡数字。
下面是上述方法的实现:
C++
// C++ implementation to check if a
// number is a digitally balanced number
#include
using namespace std;
// Function to check if the digits in the
// number is the same number of digits
int checkSame(int n, int b)
{
map m;
// Loop to iterate over the
// digits of the number N
while (n != 0) {
int r = n % b;
n = n / b;
m[r]++;
}
int last = -1;
// Loop to iterate over the map
for (auto i = m.begin(); i != m.end(); i++) {
if (last != -1 && i->second != last) {
return false;
}
else {
last = i->second;
}
}
}
// Driver Code
int main()
{
int n = 9;
int base = 2;
// function to check
if (checkSame(n, base))
cout << "Yes";
else
cout << "NO";
return 0;
}
Java
// Java implementation to check if a
// number is a digitally balanced number
import java.util.*;
class GFG{
// Function to check if the digits in the
// number is the same number of digits
static boolean checkSame(int n, int b)
{
HashMap m = new HashMap();
// Loop to iterate over the
// digits of the number N
while (n != 0)
{
int r = n % b;
n = n / b;
if(m.containsKey(r))
{
m.put(r, m.get(r) + 1);
}
else
{
m.put(r, 1);
}
}
int last = -1;
// Loop to iterate over the map
for (Map.Entry i : m.entrySet())
{
if (last != -1 && i.getValue() != last)
{
return false;
}
else
{
last = i.getValue();
}
}
return true;
}
// Driver Code
public static void main(String[] args)
{
int n = 9;
int base = 2;
// function to check
if (checkSame(n, base))
System.out.print("Yes");
else
System.out.print("NO");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation to check if a
# number is a digitally balanced number
# Function to check if the digits in the
# number is the same number of digits
def checkSame(n, b):
m = {}
# Loop to iterate over the
# digits of the number N
while (n != 0):
r = n % b
n = n // b
if r in m:
m[r] += 1
else:
m[r] = 1
last = -1
# Loop to iterate over the map
for i in m:
if last != -1 and m[i] != last:
return False
else:
last = m[i]
return True
# Driver code
n = 9
base = 2
# Function to check
if (checkSame(n, base)):
print("Yes")
else:
print("NO")
# This code is contributed by divyeshrabadiya07
C#
// C# implementation to check if a
// number is a digitally balanced number
using System;
using System.Collections.Generic;
class GFG{
// Function to check if the digits in the
// number is the same number of digits
static bool checkSame(int n, int b)
{
Dictionary m = new Dictionary();
// Loop to iterate over the
// digits of the number N
while (n != 0)
{
int r = n % b;
n = n / b;
if(m.ContainsKey(r))
{
m[r] = m[r] + 1;
}
else
{
m.Add(r, 1);
}
}
int last = -1;
// Loop to iterate over the map
foreach(KeyValuePair i in m)
{
if (last != -1 && i.Value != last)
{
return false;
}
else
{
last = i.Value;
}
}
return true;
}
// Driver Code
public static void Main(String[] args)
{
int n = 9;
int Base = 2;
// Function to check
if (checkSame(n, Base))
Console.Write("Yes");
else
Console.Write("NO");
}
}
// This code is contributed by 29AjayKumar
输出:
Yes
参考: https : //oeis.org/A031443