实现循环关系

Implementing a recurring relationship

我正在尝试通过递归函数计算表达式 y = (1 + 4 + 7 + ... + 301) / (2 + 5 + 8 + ... + 272),但是我写的不行。谁能告诉我我做错了什么? 此版本 returns 错误 >Process finished with exit code -1073741571 (0xC00000FD)

这是我的代码

int calcSum(int start, int end, int steep){
    if(start == 0){
        return 0;
    } else{
        return start + calcSum(start,end,steep);
    }
}
int main() {

    printf("y = %d", calcSum(1,301,3)/ calcSum(1,272,3));
    return 0;
}

问题在于你的递归调用,你没有做任何事情来确保函数停止调用并因此中断。当你说你想递归地添加数字时,你需要做三件事:调用相同的函数(显然),有一个终止条件,在这种情况下实际传递值,以便在每次调用中它按顺序添加需要。

#include <stdio.h>

int calcSum(int start, int end, int steep){
 // Stop calling once you have added all numbers
        if(start<=end){
            return start + calcSum(start+steep,end,steep); // increment start with steep so that in each call the next term in the sequence is added.
        }
        else return 0; //return 0 after start > end as we don't need to add after that.
}
int main(void) {
    printf("y = %d", calcSum(1,301,3)/ calcSum(2,272,3));
    return 0;
}

这是工作代码的样子。