阿姆斯壮计算功能崩溃

时间:2017-09-02 03:14:21

标签: c++

我正在用c ++编写一个函数,它计算所有作为函数参数发送的上限的武器数。出于某种原因,我的功能并没有将153识别为一个非常强的数字,并且在识别出9474后它崩溃了。这是我到目前为止所做的:

void isArmstrong(int curr_value, int arm) {

    //tmp value to hold original upper limit
    int curr_value1 = curr_value;

    //accumulator to keep track of how many armstrong numbers have been found
    int armstrong = 0;

    //determining how many digits long the current tested number is for armstrong testing
    string currStr = to_string(curr_value);

    //the power the current tested number will be raised to
    int nth = currStr.length();

    //cout <<"num " << armStr<<endl<<"nth "<<nth << endl;

    //variable to break out of loop
    int accum = 0;

    //sum variable to check if the number is armstrong after computational testing
    int sum = 0;

    //temp number to add to the variable 'sum'
    int tmp = 0;

    //cout <<"-----"<< curr_value << endl;

    while (accum <= nth) {
        //calculating a numbers sum by raising each digit to the nth power and adding that to sum
        int digit = curr_value1 % 10;
        curr_value1 = (curr_value1 / 10);

        tmp = pow(digit, nth);

        sum += tmp;

        accum += 1;

    }

    //if sum and the current value are the same, the number is an armstrong number
    if (sum == curr_value) {
        armstrong += 1;
        cout << sum << endl;
    }

    //making sure the current value is less than the upper limit and calling the function again
    if (curr_value < arm) {
        isArmstrong(curr_value += 1, arm);
    }

}

以下是我从main调用函数的方法:

isArmstrong(1, 54748);

这是控制台输出

1
2
3
4
5
6
7
8
9
370
371
407
1634
8208
9474

1 个答案:

答案 0 :(得分:2)

而是进行过多的递归调用,这有可能使您的堆栈满,请执行此操作。

for(int i=1;i<=54748;i++) if(check_armstrong(i)){//..do your work };

我的建议只是进行迭代调用,而不是进行太多的递归调用。

太多的递归调用会使程序停止,因为堆栈会被调用的函数帧填满。这就是为什么你不应该依赖于深度递归而应该使用独立于堆栈内存的迭代版本。

每次调用函数时,都需要在内存中存储有关该函数的一些数据。因此,只有一定数量的调用可以适合内存。这里当您将函数调用9474次时,内存变满。这就是它停止的原因。

额外点:

  1. 需要检查条件。 while(accum<nth)

  2. 您将阿姆斯特朗号码的数量存储在局部变量中。如果你想使用它,你可以使用全局变量(不推荐),或者你可以简单地将它保留在函数之外,并单独从函数本身使用它。

  3. 函数应该是模块化的,只执行一个操作。在我建议的解决方案中,您可以看到,您可以多次重用check-armstrong函数,这在理想情况下是使用函数的本质。

    提示:

    bool check_armstrong(int curr_value) {
    
        //tmp value to hold original upper limit
        int curr_value1 = curr_value;
    
    
        //determining how many digits long the current tested number is for armstrong testing
        string currStr = to_string(curr_value);
    
        //the power the current tested number will be raised to
        int nth = currStr.length();
    
        //variable to break out of loop
        int accum = 0;
    
        //sum variable to check if the number is armstrong after computational testing
        int sum = 0;
    
        //temp number to add to the variable 'sum'
        int tmp = 0;
    
    
        while (accum < nth) {
            //calculating a numbers sum by raising each digit to the nth power and adding that to sum
            int digit = curr_value1 % 10;
            curr_value1 = (curr_value1 / 10);
    
            tmp = pow(digit, nth);
    
            sum += tmp;
    
            accum += 1;
    
        }
    
        //if sum and the current value are the same, the number is an armstrong number
        if (sum == curr_value) {
            return true
        }
        return false;
    
    }
    

    在循环中你可以这样做

    for( int i =1;i<=... )
    {
       if( check_armstrong(i))
       {
          cout<<i<<endl;
          armstrong_count++;
       }
    }