📌  相关文章
📜  检查数字N是否以b为基础以1开头

📅  最后修改于: 2021-05-04 16:13:58             🧑  作者: Mango

给定数字N和基数b,如果基数b表示中的N以1打印开始,则为其他,打印否
例子 :

输入:n = 6,b = 4输出:是6可以写成12_4在基数4中,所以答案是肯定的,因为它以1开头输入:n = 24,b = 2输出:是,可以将24表示为11000_2在基数2中,所以答案是肯定的,因为它以1开头输入:n = 24,b = 7输出:否24可以写为33_7在基数7中,所以答案是“否”,因为它以3开头

当数字N以基数’b’表示时,它将转换为m + 1个长度序列b_{m}b_{m-1} ….. b_0这意味着b_{m} * b^{m} + b_{m-1} * b^{m-1} ….. + b_0 * b^0 = N
基数b中最小的数字,以’1’开头,即基数为100..00和m + 1个数字为b^{m}
最大数量是2 * b^{m} -1。因此N应该在此范围内。
b^{m} <= N <= 2 * b^{m} -1
现在要注意的另一点是,m不能超过楼板数( log_2 (N))因为当我们在base-2中表示任何数字时,它将转换为仅1和0的序列,因此此序列的长度将始终大于任何其他基本表示形式,并且其长度将等于floor( log_2 ((N))+ 1。
因此,要检查给定的基数“ b”(如果N开头不是1),我们将从m = 1遍历到m = floor( log_2 (N)),并检查是否有m N处于该范围内b^{m} <= N <= 2 * b^{m} -1或否,并因此打印“是”或“否”。

C++
// C++ program to check if number starts with
// one in base b representation
#include 
using namespace std;
  
// Returns true if n starts with  1 in
// base b represenation
bool CheckIfstartsWithOne(int n, int b)
{
    // highest m can be log2(n)
    int m = log2(n);
    for (int i = 1; i <= m; i++) {
        // if b^m <= N <= 2*b^m - 1,
        // return true
        if (n >= pow(b, i) && n <= 2 * pow(b, i) - 1)
            return true;
    }
    return false;
}
  
// printing yes or no
void printYesORno(int n, int b)
{
    if (CheckIfstartsWithOne(n, b) == true)
        cout << "Yes" << endl;
    else if (CheckIfstartsWithOne(n, b) == false)
        cout << "No" << endl;
}
  
// driver function
int main()
{
    printYesORno(6, 4);
    printYesORno(24, 2);
    printYesORno(24, 7);
    printYesORno(24, 15);
}


Java
// Java program to check if number starts with
// one in base b representation
  
class GFG {
      
    // Returns true if n starts with 1 in
    // base b represenation
    static boolean CheckIfstartsWithOne(int n, int b)
    {
          
        // highest m can be log2(n)
        int m = (int)(Math.log10(n) / Math.log10(2));
        for (int i = 1; i <= m; i++) {
              
            // if b^m <= N <= 2*b^m - 1,
            // return true
            if (n >= (int)Math.pow(b, i) &&
                n <= 2 * (int)Math.pow(b, i) - 1)
                return true;
        }
          
        return false;
    }
  
    // Driver method
    public static void main(String args[])
    {
        System.out.println(
           CheckIfstartsWithOne(6, 4) ? "Yes" : "No");
             
        System.out.println(
           CheckIfstartsWithOne(24, 2) ? "Yes" : "No");
             
        System.out.println(
           CheckIfstartsWithOne(24, 7) ? "Yes" : "No");
             
        System.out.println(
           CheckIfstartsWithOne(24, 15) ? "Yes" : "No");
    }
}


Python3
# Python3 program to check 
# if number starts with one
# in base b representation
import math
  
# Returns true if n 
# starts with 1 in 
# base b represenation
def CheckIfstartsWithOne(n, b):
      
    # highest m can be log2(n)
    m = (int)(math.log2(n));
    for i in range (1, m + 1):
          
        # if b^m <= N <= 2*b^m - 1,
        #return true
        x = (int)(math.pow(b, i));
        if n >= x and n <= 2 * x - 1:
            return 1;
    return 0;
  
# printing yes or no
def printYesORno(n, b):
    if CheckIfstartsWithOne(n, b) == 1:
        print("Yes");
    if CheckIfstartsWithOne(n, b) == 0:
        print("No");
          
# Driver Code
printYesORno(6, 4);
printYesORno(24, 2);
printYesORno(24, 7);
printYesORno(24, 15);
  
# This code is contributed by mits.


C#
// C# program to check if number starts
// with one in base b representation
using System;
  
public class GFG {
      
    // Returns true if n starts with
    // 1 in base b represenation
    static bool CheckIfstartsWithOne(int n, int b)
    {
          
        // highest m can be log2(n)
        int m = (int)(Math.Log10(n) / Math.Log10(2));
          
        for (int i = 1; i <= m; i++)
        {
              
            // if b^m <= N <= 2*b^m - 1,
            // return true
            if (n >= (int)Math.Pow(b, i) && 
                   n <= 2 * (int)Math.Pow(b, i) - 1)
                return true;
        }
          
        return false;
    }
  
    // Driver method
    public static void Main()
    {
        Console.WriteLine(
           CheckIfstartsWithOne(6, 4) ? "Yes" : "No");
             
        Console.WriteLine(
           CheckIfstartsWithOne(24, 2) ? "Yes" : "No");
             
        Console.WriteLine(
           CheckIfstartsWithOne(24, 7) ? "Yes" : "No");
             
        Console.WriteLine(
           CheckIfstartsWithOne(24, 15) ? "Yes" : "No");
    }
}
  
// This code is contributed by Sam007.


PHP
= pow($b, $i) && 
                $n <= 2 * pow($b, $i) - 1)
            return true;
    }
    return false;
}
  
// printing yes or no
function printYesORno($n, $b)
{
    if (CheckIfstartsWithOne($n, $b) == true)
        echo "Yes" ,"\n";
    else if (CheckIfstartsWithOne($n, $b) == false)
        echo "No" ,"\n";
}
  
// Driver Code
printYesORno(6, 4);
printYesORno(24, 2);
printYesORno(24, 7);
printYesORno(24, 15);
  
// This code is contributed by ajit 
?>


输出 :

Yes
Yes
No
Yes