C++ 简单C++;生成阿姆斯特朗数的代码
下面是我简单的尝试。但它只输出“1”。可能有什么问题C++ 简单C++;生成阿姆斯特朗数的代码,c++,sequences,C++,Sequences,下面是我简单的尝试。但它只输出“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 Armstro
#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;
}
#包括
#包括
#包括
int main()
{
clrsc();
INTR;
long int num=0,i,sum=0,temp;
cout>num;
库特
阿姆斯特朗数:n位数字等于其数字的n次幂之和
从你的代码
sum = sum + r*r*r;
“r*r*r”不是数字的n次方
阿姆斯特朗数:n位数字等于其数字的n次幂之和
从你的代码
sum = sum + r*r*r;
“r*r*r”不是数字的n次方。您需要始终在for-i循环内设置sum=0
。您需要始终在for-i循环内设置sum=0
。您可以使用日志计算n:
n = log(i)+1
然后正确计算r^n
并将其用于求和:sum+=r^n;
。r*r*r不是正确的计算方法。您可以使用log计算n:
n = log(i)+1
然后正确计算r^n
,并在求和时使用它:sum+=r^n;
。r*r*r不是正确的计算方法。第一件事是假设n(如n次方)总是三(在r*r
中)。只有当初始值有三位数时才是正确的(与153示例一样)
您需要计算初始数字中的数字以计算n,然后用将r提高到n次方来替换r*r*r
但这并不能解释为什么找不到153。原因是除非找到匹配项,否则不会将求和重置为零。无论是否找到匹配项,都需要将其重置为零。第一件事是假设n(如第n次方)总是三(在r*r*r
中)。只有当初始值有三位数字时才是如此(如153示例)
您需要计算初始数字中的数字以计算n,然后用将r提高到n次方来替换r*r*r
但是,这并不能解释为什么找不到153。原因是除非找到匹配项,否则不会将sum重置为零。无论是否找到匹配项,都需要将其重置为零。您的代码仅适用于n=3
:sum=sum+r*r*r;
您必须使用pow()
函数()来计算功率(或创建自定义功率)。您的代码仅适用于n=3
:sum=sum+r*r*r;
您必须使用pow()
函数()计算功率。(或创建自定义功率)。总结正确的部分答案:
// #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;
}
}
/#包括
对于(long int i=1;i,总结正确但部分的答案:
// #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;
}
}
/#包括
对于(long int i=1;i@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();
}
#包括
#包括
阶级武装
{
INTA;
公众:
void display();
};
void arm::display()
{
库塔;
对于(int i=a;i>=1;i--)
{
int d=i;
int b=i;
int c=i;
整数计数=0;
而(b!=0)
{
b=b/10;
计数++;
}
int l,m;
m=0;
对于(int k=1;k@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();
}
#包括
#包括
阶级武装
{
INTA;
公众:
void display();
};
void arm::display()
{
库塔;
对于(int i=a;i>=1;i--)
{
int d=i;
int b=i;
int c=i;
整数计数=0;
而(b!=0)
{
b=b/10;
计数++;
}
int l,m;
m=0;
对于(int k=1;klook仔细看他设置的是‘sum=0’@Ashot:不总是,只有当
i==sum
时。好的,我明白了。所以我只是在i==sum变为真的时候才重置sum。我的错误。仔细看,他设置的是‘sum=0’@Ashot:不总是,只有在
i==sum
的时候。好的,我明白了。所以我只是在i==sum变为真的时候重置了。我的错误柯。只是一个建议,你可能想限制逗号运算符的使用。我的问题都解决了,但答案都分散了。哪一个应该是正确的答案?只是一个建议,你可能想限制逗号运算符的使用。我的问题都解决了,但答案都分散了。哪一个应该是正确的答案?嗯,严格来说ly,它对值1有效,因为1到n次方总是1,与n的值无关。严格地说,它对值1有效,因为1到n次方总是1,与n的值无关。@power Inside不正确对不起,是的,有问题。.谢谢你指出。.我很抱歉hasty@Power-里面看看my回答。使用log
函数计算n
。@电源内部不正确抱歉是的,有问题..谢谢你指出..我很高兴hasty@Power-查看我的答案。使用log
函数计算n
。log(i)在C和C++中,从<代码> <代码>是对数基e=2.718…,而不是基10!是的,谢谢,我一直在用3位限制数调试我的代码,所以这才不会注意到problem@rene那么,我应该如何处理日志函数呢?我以前从未用过C++。如果您可以使用<代码> Log10或< Co,请建议。de>log(i)/log(10)