C++ 2算法的cpp幂
所以我希望这段代码能够精确计算2的幂次数,但它不会停止,直到我在控制台中输入奇数,我的代码有任何修正吗?提前非常感谢C++ 2算法的cpp幂,c++,math,C++,Math,所以我希望这段代码能够精确计算2的幂次数,但它不会停止,直到我在控制台中输入奇数,我的代码有任何修正吗?提前非常感谢 在此处输入代码: #include <iostream> using namespace std; int main() { int a; int counter=0; cin >> a; while(true){ cin >> a; if(a%2==1)
在此处输入代码:
#include <iostream>
using namespace std;
int main()
{
int a;
int counter=0;
cin >> a;
while(true){
cin >> a;
if(a%2==1)
break;
a/=2;
counter=counter+1;
}
cout << counter;
return 0;
}
#包括
使用名称空间std;
int main()
{
INTA;
int计数器=0;
cin>>a;
while(true){
cin>>a;
如果(a%2==1)
打破
a/=2;
计数器=计数器+1;
}
你可能错过了一些东西:
- 你没有按照自己的意愿接受输入
- 你的计数程序错了
Soln:
- 如果数字
n
是2的幂,则n
和的操作
n-1
必须为0。在所有其他情况下,结果不是0
。比如
n = 4 (in binary it is 100)
n - 1 = 3 (in binary it is 11)
n & (n - 1) = 0
100 (4)
& 011 (3)
-----------
000 (0)
- 使用这个技巧
#include <iostream>
using namespace std;
int main()
{
int tests, a;
int counter = 0;
cin >> tests;
for (int i = 0; i < tests; i++)
{
cin >> a;
if ((a & (a - 1)) == 0)
counter = counter + 1;
}
cout << counter;
return 0;
}
#包括
使用名称空间std;
int main()
{
int测试,a;
int计数器=0;
cin>>测试;
对于(int i=0;i>a;
如果((a&(a-1))==0)
计数器=计数器+1;
}
你可能错过了一些东西:
- 你没有按照自己的意愿接受输入
- 你的计数程序错了
Soln:
- 如果数字
n
是2的幂,则n
和的操作
n-1
必须为0。在所有其他情况下,结果不是0
。比如
n = 4 (in binary it is 100)
n - 1 = 3 (in binary it is 11)
n & (n - 1) = 0
100 (4)
& 011 (3)
-----------
000 (0)
- 使用这个技巧
#include <iostream>
using namespace std;
int main()
{
int tests, a;
int counter = 0;
cin >> tests;
for (int i = 0; i < tests; i++)
{
cin >> a;
if ((a & (a - 1)) == 0)
counter = counter + 1;
}
cout << counter;
return 0;
}
#包括
使用名称空间std;
int main()
{
int测试,a;
int计数器=0;
cin>>测试;
对于(int i=0;i>a;
如果((a&(a-1))==0)
计数器=计数器+1;
}
cout如果你想计算所有输入的二次幂,我会使用一个double作为输入,以便得到那些小于零的指数(0.5,0.25,等等)
为此,由于双精度浮点格式(如IEEE 754-2008标准中所定义)表示双精度,因此您只需检查将数字与std::frexp()进行分解后得到的标准化分数是否等于0.5:
#include <cmath>
bool isPowerOfTwo(double a)
{
int exp;
return std::frexp(a, &exp) == 0.5;
}
#包括
bool isPowerOfTwo(双a)
{
国际贸易;
返回std::frexp(a,&exp)==0.5;
}
然后代码:
#include <cmath>
#include <iostream>
bool isPowerOfTwo(double a)
{
int exp;
return std::frexp(a, &exp) == 0.5;
}
int main() {
unsigned counter = 0;
while (true) {
double input;
std::cin >> input;
if (!isPowerOfTwo(input)) {
break;
}
counter++;
}
std::cout << "Number of inputs power of 2: " << counter << std::endl;
return 0;
}
#包括
#包括
bool isPowerOfTwo(双a)
{
国际贸易;
返回std::frexp(a,&exp)==0.5;
}
int main(){
无符号计数器=0;
while(true){
双输入;
std::cin>>输入;
如果(!isPowerOfTwo(输入)){
打破
}
计数器++;
}
如果你想计算所有输入的二次幂,我会使用一个double作为输入,以使这些指数小于零(0.5,0.25,等等)
为此,由于双精度浮点格式(如IEEE 754-2008标准中所定义)表示双精度,因此您只需检查将数字与std::frexp()进行分解后得到的标准化分数是否等于0.5:
#include <cmath>
bool isPowerOfTwo(double a)
{
int exp;
return std::frexp(a, &exp) == 0.5;
}
#包括
bool isPowerOfTwo(双a)
{
国际贸易;
返回std::frexp(a,&exp)==0.5;
}
然后代码:
#include <cmath>
#include <iostream>
bool isPowerOfTwo(double a)
{
int exp;
return std::frexp(a, &exp) == 0.5;
}
int main() {
unsigned counter = 0;
while (true) {
double input;
std::cin >> input;
if (!isPowerOfTwo(input)) {
break;
}
counter++;
}
std::cout << "Number of inputs power of 2: " << counter << std::endl;
return 0;
}
#包括
#包括
bool isPowerOfTwo(双a)
{
国际贸易;
返回std::frexp(a,&exp)==0.5;
}
int main(){
无符号计数器=0;
while(true){
双输入;
std::cin>>输入;
如果(!isPowerOfTwo(输入)){
打破
}
计数器++;
}
std::无法清楚地说明您想要什么。使用调试器逐步完成代码。这应该可以清楚地说明当您输入各种输入时代码在做什么。因此,让我澄清一下。如果我在控制台中输入3,那么我必须再输入3个数字,例如4、8和100。在这个cas中,我的代码必须输出2的幂的数字量E2我只想数一数2的幂是多少there@geeccc您的代码不会检查数字是否为2的幂。您的代码会检查数字是否可以除以2。因此,它会将“6”计为“2的幂”。明确你想要什么。使用调试器逐步完成代码。这应该可以明确当你输入各种输入时代码在做什么。所以让我明确一下。如果我在控制台中输入3,那么我必须再输入3个数字,例如4、8和100。我的代码必须输出2的幂的数字量,在这种情况下,2I只是想数一数2的幂是多少there@geeccc您的代码不会检查数字是否为2的幂。您的代码会检查数字是否可以除以2。因此,它会将“6”计为“2的幂”。您能否展开您的答案?与其只发布代码,不如解释原始代码的错误以及您更改了哪些内容来修复它。您能否展开您的答案?不如只发布代码,解释原始代码的错误以及您更改了哪些内容来修复它。