c++ - 函数计算对数

标签 c++ c

<分区>

我写我的程序计算:

#include <stdio.h>

int cal(int a, int b){
    if (a == 0){
        return 1;
    }
    else if (a == 1){
        return b;
    }
    else{
        int c = a / b;
        return (cal(a, c) + 1);
    }
}

int main(){
    printf("Enter values: ");
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", cal(a, b));
    return 0;
}

但是当我运行我的程序时。我有一个错误。 所以。我的程序错了,或者 C、C++ 编程有什么问题? 感谢您查看我的问题。

最佳答案

我很好!谢谢大家。这是我的失败。 我改变了我的程序:

#include <stdio.h>

int cal(int a, int b){
    if (a == 0){

        return 0;
    }
    else if (a == 1){
        return b;
    }
    else if (b == 0){
        return 1;
    }
    else if (b == 1){
        return 0;
    }
    else{
        return (cal(a, b/a) + 1);
    }
}

int main(){
    printf("Enter values: ");
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", cal(a, b));
    return 0;
}

关于c++ - 函数计算对数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33568043/

相关文章:

c++ - 为什么允许 std::atomic_{char,schar,etc.} typedef 是 std::atomic<T> 基类的类型定义,而不仅仅是 atomic<T>?

c++ - 使用 stringstream 对字符串进行标记,其中最后一个字符是分隔符

c - C 中的管道 - 我必须使用 fork 吗?

c - 如何用 NULL 初始化结构指针变量?

gcc 编译器的 C shell 脚本

c - 为什么中断服务程序PUSH {r3,r4,r5,lr}但POP {r0,r4,r5,lr}会导致ERROR?

c++ - 如何实现返回 protected 结构的私有(private)函数

c++ - C++ 检测惯用法在 Visual Studio 2015 Update 2 CTP 中未按预期工作

c++ - 在 C++ 中使用 Bash 终端 (WSL) 的 Visual Studio Code 仅构建 .out 文件 - 而不是 .exe

c - 函数 'memcpy' 的警告可能数据溢出是如何发生的以及如何修复它?