📜  检查N是否包含所有数字,例如基数B中的K

📅  最后修改于: 2021-04-24 22:24:10             🧑  作者: Mango

给定三个数字NKB ,任务是检查N在基数B中是否仅包含K作为数字。

例子:

天真的方法:一个简单的解决方案是将给定的数字N转换为基数B,并一一检查其所有数字是否均为K。

时间复杂度: O(D),其中D是数字N中的位数
辅助空间: O(1)

有效途径:在问题的关键发现是,任何数量的在碱B中的所有位为K可以表示为:

这些项采用“几何级数”的形式,其中第一项为K,公共比率为B。

GP系列的总和:

因此,基数为B且所有数字均为K的数字为:

因此,只需检查该总和是否等于N。如果相等,则打印“是”,否则打印“否”

下面是上述方法的实现:

C++
// C++ implementation of the approach 
#include
using namespace std;
  
// Function to print the number of digits 
int findNumberOfDigits(int n, int base)
{
      
    // Calculate log using base change 
    // property and then take its floor 
    // and then add 1 
    int dig = (floor(log(n) / log(base)) + 1); 
  
    // Return the output 
    return (dig); 
}
  
// Function that returns true if n contains 
// all one's in base b 
int isAllKs(int n, int b, int k)
{ 
    int len = findNumberOfDigits(n, b); 
  
    // Calculate the sum 
    int sum = k * (1 - pow(b, len)) / 
                  (1 - b); 
    if(sum == n)
    {
        return(sum); 
    }
}
  
// Driver code 
int main()
{
      
    // Given number N 
    int N = 13;
      
    // Given base B 
    int B = 3;
      
    // Given digit K 
    int K = 1;
      
    // Function call 
    if (isAllKs(N, B, K))
    { 
        cout << "Yes";
    } 
    else
    {
        cout << "No";
    }
}
  
// This code is contributed by vikas_g


C
// C implementation of the approach
#include 
#include 
  
// Function to print the number of digits 
int findNumberOfDigits(int n, int base)
{
      
    // Calculate log using base change 
    // property and then take its floor 
    // and then add 1 
    int dig = (floor(log(n) / log(base)) + 1); 
  
    // Return the output 
    return (dig); 
}
  
// Function that returns true if n contains 
// all one's in base b 
int isAllKs(int n, int b, int k)
{ 
    int len = findNumberOfDigits(n, b); 
  
    // Calculate the sum 
    int sum = k * (1 - pow(b, len)) / 
                  (1 - b); 
    if(sum == n)
    {
        return(sum);
    }
}
  
// Driver code 
int main(void)
{
      
    // Given number N 
    int N = 13;
      
    // Given base B 
    int B = 3;
      
    // Given digit K 
    int K = 1;
      
    // Function call 
    if (isAllKs(N, B, K))
    { 
        printf("Yes");
    } 
    else
    {
        printf("No");
    }
    return 0;
}
  
// This code is contributed by vikas_g


Java
// Java implementation of above appraoch
import java.util.*;
  
class GFG{
  
// Function to print the number of digits
static int findNumberOfDigits(int n, int base)
{
      
    // Calculate log using base change
    // property and then take its floor
    // and then add 1
    int dig = ((int)Math.floor(Math.log(n) /
                    Math.log(base)) + 1);
      
    // Return the output
    return dig;
}
  
// Function that returns true if n contains
// all one's in base b
static boolean isAllKs(int n, int b, int k)
{
    int len = findNumberOfDigits(n, b);
      
    // Calculate the sum
    int sum = k * (1 - (int)Math.pow(b, len)) / 
                  (1 - b);
      
    return sum == n;
}
  
// Driver code
public static void main(String[] args)
{
      
    // Given number N
    int N = 13;
      
    // Given base B
    int B = 3;
      
    // Given digit K
    int K = 1;
      
    // Function call
    if (isAllKs(N, B, K))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
import math
  
# Function to print the number of digits
def findNumberOfDigits(n, base):
  
    # Calculate log using base change
    # property and then take its floor
    # and then add 1
    dig = (math.floor(math.log(n) /
                      math.log(base)) + 1)
  
    # Return the output
    return dig
  
# Function that returns true if n contains
# all one's in base b
def isAllKs(n, b, k):
  
    len = findNumberOfDigits(n, b)
  
    # Calculate the sum
    sum = k * (1 - pow(b, len)) / (1 - b)
  
    return sum == N
  
# Driver code
  
# Given number N
N = 13
  
# Given base B
B = 3
  
# Given digit K
K = 1
  
# Function call
if (isAllKs(N, B, K)):
    print("Yes")
else:
    print("No")


C#
// C# implementation of above appraoch
using System;
  
class GFG{ 
      
// Function to print the number of digits 
static int findNumberOfDigits(int n, int bas) 
{ 
      
    // Calculate log using base change 
    // property and then take its floor 
    // and then add 1 
    int dig = ((int)Math.Floor(Math.Log(n) / 
                               Math.Log(bas)) + 1);
      
    // Return the output 
    return dig;
} 
  
// Function that returns true if n contains 
// all one's in base b 
static bool isAllKs(int n, int b, int k) 
{ 
    int len = findNumberOfDigits(n, b); 
      
    // Calculate the sum 
    int sum = k * (1 - (int)Math.Pow(b, len)) / 
                  (1 - b); 
      
    return sum == n; 
} 
  
// Driver code
public static void Main()
{
      
    // Given number N 
    int N = 13; 
      
    // Given base B 
    int B = 3; 
      
    // Given digit K 
    int K = 1; 
      
    // Function call 
    if (isAllKs(N, B, K)) 
        Console.Write("Yes"); 
    else
        Console.Write("No"); 
}
}
  
// This code is contributed by vikas_g


输出:
Yes

时间复杂度: O(log(D)),其中D是数字N中的位数
辅助空间: O(1)