我只是想了解2的最小幂算法是如何工作的,特别是这段代码,结果*= 2是如何从用户输入的数字中返回最小幂2的。我似乎无法弄清楚,即使我手动写出它。任何帮助,以澄清将不胜感激!
int main()
{
int userInput, result;
do
{
printf("Enter a number (-1 to exit): ");
scanf("%d", &userInput);
if (userInput > 0)
{
result = 1;
while (result < userInput)
{
result *= 2;
}
printf("Minimum power of 2 greater than %d: %d\n", userInput, result);
}
} while (userInput != -1);
return EXIT_SUCCESS;
}输入3时输出:Min.3:4
输入5时输出:分钟.5:8
发布于 2022-01-19 19:40:48
改为:
while (result < userInput)
{
printf("Before: %d\n");
result *= 2;
printf("After: %d\n\n");
}你会看到发生了什么。
https://stackoverflow.com/questions/70776626
复制相似问题