我必须编写一个程序,它接受两个正整数,start和end,其中(1 < start < end).然后程序将在此范围内[ 开始,结束 ]并计算3的幂数.
因此,例如,如果start为2且end为10,则输出为2.(3和9为3的幂).
以下是我的代码:
#include <stdio.h>
#include <math.h>
int main(void) {
int start, end, i, count = 0;
printf("Enter start and end: ");
scanf("%d %d", &start, &end);
for (i = start; i <= end; i++) {
if ((log(i) / log(3)) == floor((log(i) / log(3)))) {
printf("%d\n", i);
count++;
}
}
printf("Answer = %d\n", count);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行其中一个测试用例[3,1000]时,输出为5,当它应为6时.
3
9
27
81
729
Answer …Run Code Online (Sandbox Code Playgroud) c ×1