阿姆斯特朗计算函数崩溃 < >我在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); } }

阿姆斯特朗计算函数崩溃 < >我在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); } },c++,C++,这里是控制台输出 1 2 3 4 5 6 7 8 9 370 371 407 1634 8208 9474 相反,进行过多的递归调用可能会使堆栈充满,请执行此操作。 for(int i=1;i错误是什么?没有调试消息。控制台挂起,然后显示armstrong.exe已停止工作。我已根据我的建议修改了我的答案。您可以查看它。谢谢,这非常有帮助。 1 2 3 4 5 6 7 8 9 370 371 407 1634 8208 9474 bool check_armstrong(int curr_v

这里是控制台输出

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

相反,进行过多的递归调用可能会使堆栈充满,请执行此操作。


for(int i=1;i错误是什么?没有调试消息。控制台挂起,然后显示armstrong.exe已停止工作。我已根据我的建议修改了我的答案。您可以查看它。谢谢,这非常有帮助。
1
2
3
4
5
6
7
8
9
370
371
407
1634
8208
9474
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++;
   }
}