简单的 C++生成阿姆斯特朗数的代码

发布于 2024-11-25 04:24:02 字数 805 浏览 4 评论 0原文

以下是我生成阿姆斯特朗数字的简单尝试。但它只输出“1”。可能出了什么问题?

#include<stdio.h> 
#include<conio.h> 
#include<iostream.h>

int main() 
{ 
    clrscr();
    int r; 
    long int num = 0, i, sum = 0, temp; 

    cout << "Enter the maximum limit to generate Armstrong number "; 
    cin >> num;
    cout << "Following armstrong numbers are found from 1 to " << num << "\t \n"; 

    for(i=1;i<=num;i++) 
    { 
        temp = i; 
        while( temp != 0 ) 
        { 
            r = temp%10; 
            sum = sum + r*r*r; 
            temp = temp / 10; 
        } 

        if ( i == sum ) {
            cout << i;
            sum = 0; 
        }
    } 

    getch(); 

    return 0; 
}

The following is my simple attempt at generating Armstrong numbers. But it only outputs "1". What might be wrong?

#include<stdio.h> 
#include<conio.h> 
#include<iostream.h>

int main() 
{ 
    clrscr();
    int r; 
    long int num = 0, i, sum = 0, temp; 

    cout << "Enter the maximum limit to generate Armstrong number "; 
    cin >> num;
    cout << "Following armstrong numbers are found from 1 to " << num << "\t \n"; 

    for(i=1;i<=num;i++) 
    { 
        temp = i; 
        while( temp != 0 ) 
        { 
            r = temp%10; 
            sum = sum + r*r*r; 
            temp = temp / 10; 
        } 

        if ( i == sum ) {
            cout << i;
            sum = 0; 
        }
    } 

    getch(); 

    return 0; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

三生一梦 2024-12-02 04:24:02

您需要始终在 for-i-loop 内设置 sum = 0

You need to always set sum = 0 inside the for-i-loop.

Smile简单爱 2024-12-02 04:24:02

阿姆斯特朗数:n 位数字等于其数字的 n 次方之和。

从你的代码来看

sum = sum + r*r*r;

'r*r*r' 不是数字的 n 次方。

Armstrong numbers: n-digit numbers equal to sum of n-th powers of their digits.

From your code

sum = sum + r*r*r;

'r*r*r' isn't n'th power of the number.

惜醉颜 2024-12-02 04:24:02

首先,您假设 n(如 n 次方)始终为 3(在您的 r*r*r 中)。仅当您的初始值具有三位数时(如 153 示例),这才是正确的。

您需要计算初始数字中的位数来计算 n,然后将您的 r*r*r 替换为 r 的 n 次方。

但这并不能解释为什么没有找到 153。原因是您不会将 sum 重置为零除非找到匹配项。无论是否找到匹配项,您都需要将其重置为零。

The first thing is that you're assuming that n (as in the nth power) is always three (in your r*r*r). That's only true if your initial value has three digits (as with the 153 example).

You need to count the digits in your initial number to calculate n, and then replace your r*r*r with raising r to the nth power.

This doesn't explain why 153 isn't found, though. The reason for that is because you aren't reseting sum to zero unless you find a match. You need to reset it to zero whether you found a match or not.

迷途知返 2024-12-02 04:24:02

您可以使用 log 计算 n:

n = log(i)+1

然后正确计算 r^n 并在求和中使用它:sum += r^n;。 r*r*r 不是计算它的正确方法。

you can calculate n using log:

n = log(i)+1

then calculate r^n correctly and use it in your summation: sum += r^n;. r*r*r is not the correct way of calculating it.

空名 2024-12-02 04:24:02

您的代码仅适用于 n=3sum = sum + r*r*r;

您必须使用 pow() 函数 (< a href="http://www.codecogs.com/reference/c/math.h/pow.php" rel="nofollow">http://www.codecogs.com/reference/c/math.h/ pow.php)来计算能力。 (或者创建一个自定义的。)

Your code only works with n=3 : sum = sum + r*r*r;

You must use the pow() function (http://www.codecogs.com/reference/c/math.h/pow.php) to compute powers. (Or create a custom one.)

掩于岁月 2024-12-02 04:24:02

总结一下正确但部分的答案:

// #include <math.h>

for (long int i = 1; i <= num; i++) 
{ 
    long int n = 0, sum = 0;  //  <--- here 
    long ing temp = i; 
    while ( temp != 0 ) 
    { 
        ++n;
        temp /= 10; 
    } 

    temp = i; 
    while ( temp != 0 ) 
    { 
        int r = temp%10; 
        sum += int(pow(double(r), n)); // <-- here
        temp /= 10; 
    } 
    if ( i == sum ) 
    {
        cout << i;
        sum = 0; 
    }
} 

To summarize the right, but partial answers:

// #include <math.h>

for (long int i = 1; i <= num; i++) 
{ 
    long int n = 0, sum = 0;  //  <--- here 
    long ing temp = i; 
    while ( temp != 0 ) 
    { 
        ++n;
        temp /= 10; 
    } 

    temp = i; 
    while ( temp != 0 ) 
    { 
        int r = temp%10; 
        sum += int(pow(double(r), n)); // <-- here
        temp /= 10; 
    } 
    if ( i == sum ) 
    {
        cout << i;
        sum = 0; 
    }
} 
污味仙女 2024-12-02 04:24:02

@Power-inside,我看到了你的代码,很难更改你的代码并编辑它,但我编写了一个类似的代码来在给定的限制内生成阿姆斯特朗数字,并且它工作得很好。
这里是....

  #include<iostream.h>
  #include<conio.h>
    class arm
       {
    int a;
    public:
       void display();
        };
     void arm::display()
          {
             cout<<"Enter any number to find armstrong numbers less than it";
             cin>>a;
              for(int i=a;i>=1;i--)
            {
              int d=i;
              int b=i;
              int c=i;
              int count=0;
                while(b!=0)
                    {
                      b=b/10;
                      count++;
                    }
                   int l,m;
                   m=0;
                for(int k=1;k<=count;k++)
               {
                  l=c%10;
                   c=c/10;
                   m=m+l*l*l;
                }



              if(d==m)

              cout<<d<<"\t";

             }

              }


            void main()
              {
                 arm k;
                 k.display();
                 getch();
                }

@Power-inside, I saw your code, its difficult to change your code and edit it, but I have written a similar code to generate Armstrong numbers in a given limit, and it works fine.
Here it is....

  #include<iostream.h>
  #include<conio.h>
    class arm
       {
    int a;
    public:
       void display();
        };
     void arm::display()
          {
             cout<<"Enter any number to find armstrong numbers less than it";
             cin>>a;
              for(int i=a;i>=1;i--)
            {
              int d=i;
              int b=i;
              int c=i;
              int count=0;
                while(b!=0)
                    {
                      b=b/10;
                      count++;
                    }
                   int l,m;
                   m=0;
                for(int k=1;k<=count;k++)
               {
                  l=c%10;
                   c=c/10;
                   m=m+l*l*l;
                }



              if(d==m)

              cout<<d<<"\t";

             }

              }


            void main()
              {
                 arm k;
                 k.display();
                 getch();
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文