检查b基中的数字N是否以1开头

给定一个数字N和基数b,如果基数b表示中的N以1开头,则打印是,否则打印否

null

例如:

Input : n = 6, b = 4Output : Yes6 can be written as 12_4 in base 4so answer is Yes as it starts with 1Input : n = 24, b = 2Output : Yes24 can be written as 11000_2 in base 2so answer is Yes as it starts with 1Input :  n = 24, b = 7Output : No24 can be written as 33_7 in base 7so answer is No as it starts with 3        

当一个数字N以基数“b”表示时,它会被转换为m+1长度序列 b_{m}    [Tex]b_{m-1}[/Tex]…。。 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) )因为当我们用基数2表示任何数字时,它会被转换成一个只有1和0的序列,所以这个序列的长度总是大于任何其他基数表示,它的长度将等于0( log_2    (N) )+1。

所以,为了检查给定的基数“b”,如果N以1开头,我们将从m=1遍历到m=floor( log_2    (N) )并检查是否有MN在该范围内 b^{m}    <=N<=2* b^{m}    -1或否,并相应地打印“是”或“否”。

C++

// C++ program to check if number starts with
// one in base b representation
#include <bits/stdc++.h>
using namespace std;
// Returns true if n starts with  1 in
// base b representation
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 representation
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 representation
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;
class GFG{
// Returns true if n starts with 1 in
// base b representation
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 code
public static void Main(String []args)
{
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 Princi Singh


PHP

<?php
// PHP program to check if
// number starts with one
// in base b representation
// Returns true if n starts
// with 1 in base b representation
function CheckIfstartsWithOne( $n , $b )
{
// highest m can be log2(n)
$m = log( $n , 2);
for ( $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
function printYesORno( $n , $b )
{
if (CheckIfstartsWithOne( $n , $b ) == true)
echo "Yes" , "" ;
else if (CheckIfstartsWithOne( $n , $b ) == false)
echo "No" , "" ;
}
// Driver Code
printYesORno(6, 4);
printYesORno(24, 2);
printYesORno(24, 7);
printYesORno(24, 15);
// This code is contributed by ajit
?>


Javascript

<script>
// Javascript program to check if number starts
// with one in base b representation
// Returns true if n starts with
// 1 in base b representation
function CheckIfstartsWithOne(n, b)
{
// highest m can be log2(n)
let m = parseInt(Math.log10(n) / Math.log10(2), 10);
for (let i = 1; i <= m; i++)
{
// if b^m <= N <= 2*b^m - 1,
// return true
if (n >= Math.pow(b, i) && n <= 2 * Math.pow(b, i) - 1)
return true ;
}
return false ;
}
document.write(CheckIfstartsWithOne(6, 4) ? "Yes" + "</br>" : "No" + "</br>" );
document.write(CheckIfstartsWithOne(24, 2) ? "Yes" + "</br>" : "No" + "</br>" );
document.write(CheckIfstartsWithOne(24, 7) ? "Yes" + "</br>" : "No" + "</br>" );
document.write(CheckIfstartsWithOne(24, 15) ? "Yes" : "No" );
</script>


输出:

YesYesNoYes

本文由 阿尤什·贾哈 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享