You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++递归函数sevenFact返回值异常问题求助

递归函数返回值无法正确传递的问题排查

编写了如下C++代码,生成包含1000个1-100随机数的数组并写入randArray.txt文件,同时实现两个递归函数:evenOut用于统计数组内偶数的数量(用户称该函数可正常返回结果);sevenFact用于统计数组中末尾为7的数字数量。目前发现sevenFact函数内部打印的统计结果正确,但返回值无法正确传递给主函数中的变量x,导致最终输出错误,恳请帮忙排查问题。

// minicount.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

double evenOut(int arr[], int size, double count) {
    double evenCount = count;
    
    if (size == 0) {
        return evenCount;
    }

    if ((arr[size-1] % 2) == 0 ) {
        evenCount += 1;
        evenOut(arr, size - 1, evenCount);
    }
    else {
        evenOut(arr, size - 1, evenCount);
    }
}

double sevenFact(int arr[], int size, double count) {
    double sevenCount = count;
    string a = to_string(arr[size - 1]);

    if (size == 0) {
        //cout << "Final total number of 7's -> " << sevenCount << endl;
        //cout << "7 to the power of " << sevenCount << " is " << fixed << pow(7, sevenCount) << endl;
        return sevenCount;
    }
    
    if (a[a.length() - 1] == '7') {
        sevenCount += 1;
        sevenFact(arr, size - 1, sevenCount);
    }
    else {
        sevenFact(arr, size - 1, sevenCount);
    }

}


int main()
{
    const string FILE = "randArray.txt";
    const int SIZE = 1000;
    int arr[SIZE];  

    ofstream OUTPUTFILE(FILE);

    srand(time(NULL));

    for (int i = 0; i < SIZE; i++) {
        arr[i] = (rand() % 100) + 1;
    }

    if (OUTPUTFILE.is_open()) {
        for (int i = 0; i < SIZE; i++) {
            OUTPUTFILE << arr[i] << endl;
        }
    }
    else {
        cout << "ERROR: Could not open file." << endl;
    }

    cout << "EvenOut: The number of even numbers in " << FILE << " is: " << evenOut(arr, SIZE, 0) << endl;
   
    double x = sevenFact(arr, SIZE, 0);

    cout << "SevenFact: Total number of numbers ending with 7: " << x;

}

问题根源

你的两个递归函数evenOut和sevenFact都存在同一个核心问题:在递归调用的分支中,没有将递归调用的返回值向上传递给上层函数。

以sevenFact为例,当执行sevenFact(arr, size - 1, sevenCount);时,只是发起了递归调用,但没有把这个调用的结果返回出去。递归的底层分支(size == 0时)确实返回了正确的统计值,但这个值无法沿着递归调用链向上传递,导致上层函数最终没有返回值,行为完全未定义(主函数拿到的x是随机垃圾值)。

evenOut函数看似正常,只是未定义行为偶然表现出正确结果,本质上同样存在隐患,必须修复。

修复方案

在每个递归调用的分支中添加return关键字,将递归调用的结果返回给上层:

修复后的evenOut函数

double evenOut(int arr[], int size, double count) {
    double evenCount = count;
    
    if (size == 0) {
        return evenCount;
    }

    if ((arr[size-1] % 2) == 0 ) {
        evenCount += 1;
        return evenOut(arr, size - 1, evenCount); // 添加return传递递归结果
    }
    else {
        return evenOut(arr, size - 1, evenCount); // 添加return传递递归结果
    }
}

修复后的sevenFact函数

double sevenFact(int arr[], int size, double count) {
    double sevenCount = count;

    if (size == 0) {
        return sevenCount;
    }
    // 把数组元素访问移到size判断之后,避免size=0时访问arr[-1]触发越界
    string a = to_string(arr[size - 1]);
    
    if (a[a.length() - 1] == '7') {
        sevenCount += 1;
        return sevenFact(arr, size - 1, sevenCount); // 添加return传递递归结果
    }
    else {
        return sevenFact(arr, size - 1, sevenCount); // 添加return传递递归结果
    }
}

额外优化建议

  1. 避免数组越界:原sevenFact中,数组元素访问写在size == 0判断之前,当size=0时会访问arr[-1]触发未定义行为,必须调整顺序。
  2. 使用整数类型统计数量:统计数字个数用int类型更合理,没必要用double,可将函数返回值和参数count的类型改为int,减少不必要的类型转换。

内容的提问来源于stack exchange,提问作者Jon Wei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 07:07:48