关于C语言的一个问题!是初学者(

img


希望大家帮忙看一下应该怎么做!不知道应该怎么控制输入数据的范围,能不能指点一下,谢谢

用log(x)/log(b)即可

#include <stdio.h>
#include <math.h>
int main()
{
    int b;
    double x;
    printf("%.6lf",log(x)/log(b));
    return 0;
}

这道题目,需要用到 log() 函数,只要引入 math.h 头文件即可,给你看个示例就明白了:

#include <stdio.h>
#include <math.h>

int main(void)
{
    // 定义变量b,x
    int b;
    double x;
    // 读取正整数 b 和实数 x 的值
    scanf("%d%lf", &b, &x);
    // 输入值,保留6位小数
    printf("%.6lf\n", log(x)/log(b));
    return 0;
}

如果我们要控制数据输入的范围可以使用 if 语句进行判断,再稍微改一下:

#include <stdio.h>
#include <math.h>

int main(void)
{
    // 定义变量b,x
    int b;
    double x;
    // 读取正整数 b 和实数 x 的值
    scanf("%d%lf", &b, &x);
    // 判断数据的范围是否合法 
    if((b >= 2 && b <= 10) && x > 0.0D) {
        // 输入值,保留6位小数
        printf("%.6lf\n", log(x)/log(b));
    } else {
        // 对于不合法的数据作提示
        printf("您输入的数据不合法");
    }
    
    return 0;
}