判断一个整数是不是2的整数次幂
#includeiostream using namespace std; //第一种方法利用循环 bool MakeDecision(const int M,int pow) //pow表示M是2的多少次幂 { int i=2; int s=1; pow=0; if(M0) return false; if(M==0) return true; do { s*=i; pow; }while(sM); if(s==M) return tru
#include
using namespace std;
//第一种方法利用循环
bool MakeDecision(const int& M,int &pow) //pow表示M是2的多少次幂
{
int i=2;
int s=1;
pow=0;
if(M
if(M==0) return true;
do
{
s*=i;
pow++;
}while(s
else
{
pow=0;
return false;
}
}
//第二种方法,如果M/2是2的整数次幂,那么M就是2的整数次幂,递归实现
bool MakeDecision(int M,int *pow)
{
if(M
if(M%2!=0) return false;
if(M==0) return true;
else if(M==2)
{
*pow=*pow+1;
return true;
}
else
{
*pow=*pow+1;
MakeDecision(M/2,pow);
}
}
//一个是2的整数次幂的整数,它的第0,1,2...都是1,而其余位都是0,这样-M的补码有1位1的位置和M的补码1的位置是相同的
//因此M & -M 就是M
bool MakeDecision(int& M)
{
return M == (M & (-M));
}
void main()
{
cout
int M;
cin>>M;
int pow=0;
//if(MakeDecision(M,&pow)) cout
if(MakeDecision(M)) cout
else cout
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The manipulation of date and time values is an important aspect of programming, and the Python language provides a useful built-in module for this called datetime. However, in some cases, you may need to convert a DateTime object to an integer value in order to perform specific operations or calculations. There are multiple ways to convert a DateTime to an integer in Python, each with its own advantages and disadvantages. In this article, we'll take a closer look at these methods and examine when each method is appropriate to use. After reading this article, you will have a complete understanding of how to efficiently convert DateTime objects to integers in Python and be able to choose the most appropriate method for your specific programming task. Method 1: Use timestamp

In JavaScript, you can use the toFixed() function to convert an integer into a decimal. This function can convert an integer into a number with a specified number of decimal places; the syntax is "number.toFixed(x)", and the parameter "x" specifies the number of decimal places. .

PHP email detection: Determine whether the email has been sent successfully. When developing web applications, you often need to send emails to communicate with users. Whether it is registration confirmation, password reset, or sending notifications, the email function is an indispensable part. However, sometimes we cannot ensure whether the email is actually sent successfully, so we need to perform email detection and determine whether the email has been sent successfully. This article will introduce how to use PHP to implement this function. 1. Use SMTP server to send emails. First, we need to use SM

Use Java's Character.isDigit() function to determine whether a character is a numeric character. Characters are represented in the form of ASCII codes internally in the computer. Each character has a corresponding ASCII code. Among them, the ASCII code values corresponding to the numeric characters 0 to 9 are 48 to 57 respectively. To determine whether a character is a number, you can use the isDigit() method provided by the Character class in Java. The isDigit() method is of the Character class

Use Java's File.isDirectory() function to determine whether a file exists and is of directory type. In Java programming, you often encounter situations where you need to determine whether a file exists and is of directory type. Java provides the File class to operate files and directories. The isDirectory() function can help us determine whether a file is a directory type. The File.isDirectory() function is a method in the File class. Its function is to determine the current File

The regular expressions for integers are: 1. Match positive integers: ^[1-9]\d*$; 2. Match negative integers: ^-[1-9]\d*$; 3. Match positive integers and negative integers :^-?\d+$; 4. Match non-zero integers: ^(0|[1-9]\d*)$; 5. Match integers (including zero): ^-?\d+$.

How to use the isInfinite() method of the Double class to determine whether a number is infinity. In Java, the Double class is a wrapper class used to represent floating point numbers. This class provides a series of methods that can conveniently operate on floating point numbers. Among them, the isInfinite() method is used to determine whether a floating point number is infinite. Infinity refers to positive infinity and negative infinity that are so large that they exceed the range that floating point numbers can represent. In computers, the maximum value of a floating point number can be obtained through the Double class

Installing an SSL certificate on SharePoint is a critical step in securing your website and providing an encrypted connection. By following the correct installation steps, you can ensure the security of your website data, improve your ranking in search engines, and provide a better user experience for your visitors. Get an SSL Certificate Contact a trusted Certificate Authority (CA) to purchase an SSL certificate. Provide the required authentication and domain ownership verification information. After completing the verification process, you will receive the SSL certificate file. Prepare the Certificate File Open your SSL certificate file using a text editor. Copy the certificate contents to a new text file. Save the file as yourdomain.cer, making sure to change "yourdomain”
