热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

检查给定的数字是否是d的幂,其中d是2的幂

检查给定的数字是否是d的幂,其中d是2的幂原文:ht

检查给定的数字是否是 d 的幂,其中 d 是 2 的幂

原文:https://www . geesforgeks . org/check-给定-number-power-d-d-power-2/

给定一个整数 n,求它是否是 d 的幂,其中 d 本身就是 2 的幂。
例:

Input : n = 256, d = 16
Output : Yes
Input : n = 32, d = 16
Output : No

方法 1 取给定数在基数 d 上的对数,如果得到一个整数,那么这个数就是 d 的幂。
方法 2 继续将这个数除以 d,即迭代做 n = n/d。在任何迭代中,如果 n%d 变成非零且 n 不是 1,则 n 不是 d 的幂,否则 n 是 d 的幂。
方法 3(按位)
如果满足以下条件,则数字 n 是 d 的幂。
a)在 n 的二进制表示中只有一个位集(注意:d 是 2 的幂)
b)在(唯一)设置位之前的零位计数是 log 的倍数 2 (d)。
例如:对于 n = 16 (10000)和 d = 4,16 是 4 的幂,因为只有一个位设置,并且在设置位是 4 之前计数为 0,4 是 log 的倍数 2 (4)。

C++


// CPP program to find if a number is power
// of d where d is power of 2.
#include<stdio.h>
unsigned int Log2n(unsigned int n)
{
return (n > 1)? 1 + Log2n(n/2): 0;
}
bool isPowerOfd(unsigned int n, unsigned int d)
{
int count = 0;
/* Check if there is only one bit set in n*/
if (n && !(n&(n-1)) )
{
    /* count 0 bits before set bit */
    while (n > 1)
    {
    n >>= 1;
    count += 1;
    }    
    /* If count is a multiple of log2(d)
    then return true else false*/
    return (count%(Log2n(d)) == 0);
}
/* If there are more than 1 bit set
    then n is not a power of 4*/
return false;
}
/* Driver program to test above function*/
int main()
{
int n = 64, d = 8;
if (isPowerOfd(n, d))
    printf("%d is a power of %d", n, d);
else
    printf("%d is not a power of %d", n, d);
return 0;
}


Java 语言(一种计算机语言,尤用于创建网站)


// Java program to find if
// a number is power of d
// where d is power of 2.
class GFG
{
static int Log2n(int n)
{
    return (n > 1)? 1 +
    Log2n(n / 2): 0;
}
static boolean isPowerOfd(int n,
                        int d)
{
int count = 0;
/* Check if there is
only one bit set in n*/
if (n > 0 && (n &
(n - 1)) == 0)
{
    /* count 0 bits
    before set bit */
    while (n > 1)
    {
        n >>= 1;
        count += 1;
    }
    /* If count is a multiple
    of log2(d) then return
    true else false*/
    return (count %
        (Log2n(d)) == 0);
}
/* If there are more
than 1 bit set then
n is not a power of 4*/
return false;
}
// Driver Code
public static void main(String[] args)
{
    int n = 64, d = 8;
    if (isPowerOfd(n, d))
        System.out.println(n +
                    " is a power of " + d);
    else
        System.out.println(n +
                    " is not a power of " + d);
}
}
// This code is contributed by mits


Python 3


# Python3 program to find if a number
# is power of d where d is power of 2.
def Log2n(n):
    return (1 + Log2n(n / 2)) if (n > 1) else 0;
def isPowerOfd(n, d):
    count = 0;
    # Check if there is only
    # one bit set in n
    if (n and (n & (n - 1))==0):
        # count 0 bits
        # before set bit
        while (n > 1):
            n >>= 1;
            count += 1;
        # If count is a multiple of log2(d)
        # then return true else false
        return (count%(Log2n(d)) == 0);
    # If there are more than 1 bit set
    # then n is not a power of 4
    return False;
# Driver Code
n = 64;
d = 8;
if (isPowerOfd(n, d)):
    print(n,"is a power of",d);
else:
    print(n,"is not a power of",d);
# This code is contributed by mits


C


// C# program to find if
// a number is power of d
// where d is power of 2.
using System;
class GFG
{
static int Log2n(int n)
{
    return (n > 1)? 1 +
    Log2n(n / 2): 0;
}
static bool isPowerOfd(int n,
                    int d)
{
int count = 0;
/* Check if there is
only one bit set in n*/
if (n > 0 && (n & (n - 1)) == 0)
{
    /* count 0 bits
    before set bit */
    while (n > 1)
    {
    n >>= 1;
    count += 1;
    }
    /* If count is a multiple
    of log2(d) then return
    true else false*/
    return (count % (Log2n(d)) == 0);
}
/* If there are more than
1 bit set then n is not
a power of 4*/
return false;
}
// Driver Code
static void Main()
{
int n = 64, d = 8;
if (isPowerOfd(n, d))
    Console.WriteLine("{0} is a " +
                    "power of {1}",
                            n, d);
else
    Console.WriteLine("{0} is not a"+
                    " power of {1}",
                            n, d);
}
// This code is contributed by mits
}


服务器端编程语言(Professional Hypertext Preprocessor 的缩写)



// PHP program to find if a number
// is power of d where d is power of 2.
function Log2n($n)
{
    return ($n > 1)? 1 +
    Log2n($n / 2): 0;
}
function isPowerOfd($n, $d)
{
    $count = 0;
// Check if there is only
// one bit set in n
if ($n && !($n & ($n - 1)))
{
    // count 0 bits
    // before set bit
    while ($n > 1)
    {
        $n >>= 1;
        $count += 1;
    }
    /* If count is a multiple of log2(d)
    then return true else false*/
    return ($count%(Log2n($d)) == 0);
}
    /* If there are more than 1 bit set
    then n is not a power of 4*/
    return false;
}
// Driver Code
$n = 64;
$d = 8;
if (isPowerOfd($n, $d))
    echo $n," ","is a power of ", $d;
else
    echo $n," ","is not a power of ", $d;
// This code is contributed by m_kit
?>


java 描述语言


<script>
// Javascript program to find if
// a number is power of d
// where d is power of 2
function Log2n(n)
{
    return (n > 1) ? 1 +
      Log2n(n / 2) : 0;
}
// Function to count the number
// of ways to paint  N * 3 grid
// based on given conditions
function isPowerOfd(n, d)
{
    var count = 0;
    /* Check if there is
    only one bit set in n*/
    if (n > 0 && (n & (n - 1)) == 0)
    {
        /* count 0 bits
        before set bit */
        while (n > 1)
        {
            n >>= 1;
            count += 1;
        }
        /* If count is a multiple
        of log2(d) then return
        true else false*/
        return (count % (Log2n(d)) == 0);
    }
    /* If there are more
    than 1 bit set then
    n is not a power of 4*/
    return false;
}
// Driver code
var n = 64, d = 8;
if (isPowerOfd(n, d))
    document.write(n +
                " is a power of " + d);
else
    document.write(n +
                " is not a power of " + d);
// This code is contributed by Khushboogoyal499
script>

Output: 

64 is a power of 8

时间复杂度: O(log 2 n)

辅助空间: O(1)


推荐阅读
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Python如何调用类里面的方法
    本文介绍了在Python中调用同一个类中的方法需要加上self参数,并且规范写法要求每个函数的第一个参数都为self。同时还介绍了如何调用另一个类中的方法。详细内容请阅读剩余部分。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
author-avatar
夕阳的春天8989_110
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有