gpt4 book ai didi

c++ - Armstrong 计算函数崩溃

转载 作者:搜寻专家 更新时间:2023-10-31 02:09:55 26 4
gpt4 key购买 nike

我正在用 C++ 编写一个函数,计算所有阿姆斯特朗数字,直到作为参数发送给该函数的上限。出于某种原因,我的函数没有将 153 识别为 armstrong 号码,并且在识别 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

最佳答案

与其进行太多有可能使您的堆栈变满的递归调用,不如这样做。

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

简单地说,我的建议是进行迭代调用,而不是进行太多的递归调用。

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

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

加分项:

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

  2. 您将 armstrong 数字的数量存储在局部变量中。如果你想使用它,你可以使用全局变量(不推荐)或者你可以简单地将它放在函数之外并与函数本身分开使用。

函数应该是模块化的,只执行一个 Action 。在我建议的解决方案中,您可以看到,您可以重复使用 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++;
}
}

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com