阿姆斯壮的数字。打印阿姆斯特朗号码

Armstrong numbers. print armstrong numbers

我恳请那些认为这个问题已经被问过的人,请先阅读。

我需要打印 1 到 10000 之间的所有阿姆斯壮数字。我的问题是,每当我的程序 运行 达到 150 时,它就会

(1^3) + ((5^3)-1) + (0^3)

而不是

(1^3) + (5^3) + (0^3).

因此它不打印 153(这是一个 Armstrong 数字),当然是因为总和结果是 152。我不知道其他一些数字是否也在这样做。但是我确实已经检查到200,除了150-160范围内的其他数字没有问题。

这是编译错误吗。我应该重新安装我的编译器吗?目前我正在使用代码块。

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    for(int i = 0;i <= 10000;++i)
    {
        int r = i;
        int dig = 0;
        while(r != 0)
        {
            dig++;
            r /= 10;
        }
        int n = i, sum = 0;
        while(n != 0)
        {
            int d = n % 10;
            sum += pow(d, dig);
            n /= 10;
        }
        if(sum == i)
            cout << i << ' ';
    }
    cout << "\n\n\n";
    return 0;
}

您应该 运行 您的代码在调试器中。此外,您的代码无法为我编译(GCC 6),因为您使用 cout 而没有 std::using namespace std;。那么它如何在您的系统上编译呢?您也在使用 math.h,在 C++ 中您应该使用 cmath.

修复此问题后,我在 6.4.1 版 g++ 的 Fedora 24 上得到以下输出:

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

其中包含 153,因此要么是您的编译器有错误,要么是您的程序有未定义的行为,因此错误随之而来。

我查看了 definition for Armstrong numbers 并做了一个非常简短的 Python 实现:

# Copyright © 2017 Martin Ueding <dev@martin-ueding.de>
# Licensed under the MIT/Expat license.

def is_armstrong(number):
    digits = [int(letter) for letter in str(number)]
    score = sum(digit**len(digits) for digit in digits)
    return score == number

armstrong = list(filter(is_armstrong, range(10000)))
print(' '.join(map(str, armstrong)))

输出与我机器上的 C++ 程序完全匹配:

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

查看你的代码我无法发现未定义的行为,它看起来很合理。首先计算位数,然后计算总和。也许您应该尝试使用其他编译器,例如 GCC、LLVM 或 Ideone。 Code Blocks 是自带编译器还是使用系统编译器?你 运行ning 是什么操作系统?


你说你只是在学习编程。很高兴听到!我希望你有 good C++ book 或其他资源。对于 C++,网上有很多不好的建议。还要确保你有一本至少有 C++11 的书,其他的都已经过时了。

我已经更改了您的程序并创建了一些仅执行一项任务的简短函数,这样更易​​于阅读和推理。我不确定您是否已经了解函数,所以如果现在看起来很复杂,请不要担心:-)。

#include <cmath>
#include <iostream>

int get_digit_count(int const number) {
    int digits = 0;
    int remainder = number;

    while (remainder > 0) {
        ++digits;
        remainder /= 10;
    }

    return digits;
}

bool is_armstrong_number(int const number) {
    int const digit_count = get_digit_count(number);
    int remainder = number;
    int sum = 0;

    while (remainder > 0) {
        int const last_digit = remainder % 10;
        sum += std::pow(last_digit, digit_count);
        remainder /= 10;
    }

    return number == sum;
}

int main() {
    for (int i = 0; i <= 10000; ++i) {
        if (is_armstrong_number(i)) {
            std::cout << i << ' ';
        }
    }
    std::cout << std::endl;
}

此算法生成并打印出阿姆斯特朗数字至 999,但可以使用相同的方法轻松扩展到任何长度。

n = 1;   %initialize n, the global loop counter, to 1

for i = 1 : 10   %start i loop

    for j = 1 : 10   %start j loop

        for k = 1 : 10   %start k loop

            rightnum = mod(n, 10);   %isolate rightmost digit

            midnum = mod(fix((n/10)), 10);   %isolate middle digit 

            leftnum = fix(n/100);   %isolate leftmost digit    

        if ((n < 10))   %calulate an for single-digit n's   
            an = rightnum;     
              end

        if ((n > 9) & (n < 100))   %calculate an for 2-digit n's
            an = fix(rightnum^2 + midnum^2);         
              end

        if ((n  > 99) & (n < 1000))   %calculate an for 3-digit n's
            an = fix(leftnum^3 + midnum^3 + rightnum^3);     
              end

            if (n == an)   %if n = an display n and an
                armstrongmatrix = [n an];
                disp(armstrongmatrix);
            end

        n = n + 1;   %increment the global loop counter and continue

        end
    end

end

您可以使用数组:

#include<iostream>
using namespace std;
int pow(int, int);
int checkArm(int);
int main() {
    int range;
    cout<<"Enter the limit: ";
    cin>>range;
    for(int i{};i<=range;i++){
        if(checkArm(i))
            cout<<i<<endl;
    }
    return 0;
}
int pow(int base, int exp){
    int i{0};
    int temp{base};
    if(exp!=0)
        for(i;i<exp-1;i++)
            base = base * temp;
    else
        base=1;
    return base;
}
int checkArm(int num) {
    int ar[10], ctr{0};
    int tempDigits{num};
    while(tempDigits>0){
        tempDigits/=10;
        ctr++;
    }
    int tempArr{num}, tempCtr{ctr};
    for(int i{0};i<=ctr;i++){
        ar[i] = tempArr / pow(10,tempCtr-1);
        tempArr = tempArr % pow(10,tempCtr-1);
        tempCtr--;
    }
    int sum{};
    for(int k{};k<ctr;k++){
        sum+=pow(ar[k],ctr);
    }
    if(sum==num)
        return 1;
    else
        return 0;
}