📌  相关文章
📜  检查数字 N 是否可以以 B 为基数表示

📅  最后修改于: 2021-09-16 11:05:39             🧑  作者: Mango

给定一个数字N和任何基数B 。任务是检查N 是否可以表示为a 1 *b 0 + a 2 *b 1 + a 3 *b 2 + ….+ a 101 *b 100其中每个系数a 1 , a 2 , a 3 …a 1010、1 或 -1

例子:

方法:以下是步骤:

  • 如果N的基础B表示仅由0 和 1组成,则答案存在。
  • 如果不满足上述条件,则从低位迭代到高位,如果该位不等于0或1,则尝试从中减去B的适当幂并增加高位。
  • 如果它变得等于-1 ,那么我们可以减去这个幂数字,如果它变得等于 0 ,那么只需忽略,在其他情况下,以所需形式表示数字是不可能的。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if a number
// N can be expressed in base B
bool check(int n, int w)
{
    vector a(105);
    int p = 0;
 
    // Check if n is greater than 0
    while (n > 0) {
        a[p++] = n % w;
        n /= w;
    }
 
    // Initialize a boolean variable
    bool flag = true;
 
    for (int i = 0; i <= 100; i++) {
 
        // Check if digit is 0 or 1
        if (a[i] == 0 || a[i] == 1)
            continue;
 
        // Subtract the appropriate
        // power of B from it and
        // increment higher digit
        else if (a[i] == w
                 || a[i] == w - 1)
            a[i + 1]++;
 
        else
            flag = false;
    }
 
    return flag;
}
 
// Driver Code
int main()
{
    // Given Number N and base B
    int B = 3, N = 7;
 
    // Function Call
    if (check(N, B))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if a number
// N can be expressed in base B
static boolean check(int n, int w)
{
    int[] a = new int[105];
    int p = 0;
 
    // Check if n is greater than 0
    while (n > 0)
    {
        a[p++] = n % w;
        n /= w;
    }
 
    // Initialize a boolean variable
    boolean flag = true;
 
    for(int i = 0; i <= 100; i++)
    {
         
        // Check if digit is 0 or 1
        if (a[i] == 0 || a[i] == 1)
            continue;
 
        // Subtract the appropriate
        // power of B from it and
        // increment higher digit
        else if (a[i] == w || a[i] == w - 1)
            a[i + 1]++;
 
        else
            flag = false;
    }
    return flag;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given number N and base B
    int B = 3, N = 7;
 
    // Function call
    if (check(N, B))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
 
# Function to check if a number
# N can be expressed in base B
def check(n, w):
     
    a = [0 for i in range(105)];
    p = 0
  
    # Check if n is greater than 0
    while (n > 0):
        a[p] = n % w
        p += 1
        n //= w
         
    # Initialize a boolean variable
    flag = True
  
    for i in range(101):
          
        # Check if digit is 0 or 1
        if (a[i] == 0 or a[i] == 1):
            continue
  
        # Subtract the appropriate
        # power of B from it and
        # increment higher digit
        elif (a[i] == w or a[i] == w - 1):
            a[i + 1] += 1
        else:
            flag = False
     
    return flag
 
# Driver code
if __name__=="__main__":
     
    # Given number N and base B
    B = 3
    N = 7
  
    # Function call
    if (check(N, B)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by rutvik_56


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if a number
// N can be expressed in base B
static bool check(int n, int w)
{
    int[] a = new int[105];
    int p = 0;
 
    // Check if n is greater than 0
    while (n > 0)
    {
        a[p++] = n % w;
        n /= w;
    }
 
    // Initialize a bool variable
    bool flag = true;
 
    for(int i = 0; i <= 100; i++)
    {
         
        // Check if digit is 0 or 1
        if (a[i] == 0 || a[i] == 1)
            continue;
 
        // Subtract the appropriate
        // power of B from it and
        // increment higher digit
        else if (a[i] == w || a[i] == w - 1)
            a[i + 1]++;
 
        else
            flag = false;
    }
    return flag;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given number N and base B
    int B = 3, N = 7;
 
    // Function call
    if (check(N, B))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
Yes

时间复杂度: O(log B N)
辅助空间: O(1)