确定号码是否为阿姆斯壮号码的问题

我正在尝试检查用户提供的号码是否是一个非常强的号码 。 但是有些事情是错的,我无法弄清楚。

任何帮助表示赞赏。

代码见下文。

#include int fun(int); int main() { int x,a,b,y=0; printf("enter the number you want to identify is aN ARMSTRONG OR NOT:"); scanf("%d",&a); for(int i=1 ; i<=3 ; i++) { b = a % 10; x = fun(b); y = x+y; a = a/10; } if(y==a) printf("\narmstrong number"); else printf("\nnot an armstrong number"); return 0; } int fun(int x) { int a; a=x*x*x; return (a); } 

主要问题是您没有记录您开始使用的号码。 您将a重复除以10(结束为0),然后将0与153进行比较。这些不相等。

你的另一个问题是,你不能寻找4位数或更长的阿姆斯壮数字,也不能寻找除1之外的1位数字。你的函数fun()会更好地命名为cube() ; 在我的下面的代码中,它被重命名为power()因为它被推广为处理N位数字。


我决定,对于所考虑的功率范围,没有必要使用更复杂的power()算法 – 一个除以2等等。可以节省6-10位数,但你不能’在这种情况下衡量它。 如果用-DDEBUG编译,它包括诊断打印 – 用于向我保证我的代码工作正常。 还要注意答案回应输入; 这是确保您获得正确行为的基本技术。 我已经将代码包装成一个函数来测试一个数字是否是一个阿姆斯特朗数,这是从主程序迭代调用的。 这使得测试更容易。 我已将检查添加到scanf()以阻止问题,这是另一项重要的基本编程技术。

我检查了大部分阿姆斯特朗的数字,最高可达146511208,这似乎是正确的。 370和371对很有趣。

 #include  #include  #ifndef DEBUG #define DEBUG 0 #endif static int power(int x, int n) { int r = 1; int c = n; while (c-- > 0) r *= x; if (DEBUG) printf(" %d**%d = %d\n", x, n, r); return r; } static bool isArmstrongNumber(int n) { int y = 0; int a = n; int p; for (p = 0; a != 0; a /= 10, p++) ; if (DEBUG) printf(" n = %d, p = %d\n", n, p); a = n; for (int i = 0; i < p; i++) { y += power(a % 10, p); a /= 10; } return(y == n); } int main(void) { while (1) { int a; printf("Enter the number you want to identify as an Armstrong number or not: "); if (scanf("%d", &a) != 1 || a <= 0) break; else if (isArmstrongNumber(a)) printf("%d is an Armstrong number\n", a); else printf("%d is not an Armstrong number\n", a); } return 0; } 

一个问题可能是您正在更改a (因此它将不再具有原始值)。 它也只匹配1, 153, 370, 371, 407 。 这是一个提示替换for和test直到a为零,并将函数更改为提高到位数。

 #include #include  int power(int, int); int numberofdigits(int); //Routine to test if input is an armstrong number. //See: http://en.wikipedia.org/wiki/Narcissistic_number if you don't know //what that is. int main() { int input; int digit; int sumofdigits = 0; printf("enter the number you want to identify as an Armstrong or not:"); scanf("%d",&input); int candidate = input; int digitcount = numberofdigits(input); for(int i=1 ; i <= digitcount ; i++) { digit = candidate % 10; sumofdigits = sumofdigits + power(digit, digitcount); candidate = candidate / 10; } if(sumofdigits == input) printf("\n %d is an Armstrong number", input); else printf("\n %d is NOT an Armstrong number", input); return 0; } int numberofdigits(int n); { return log10(n) + 1; } int power(int n, int pow) { int result = n; int i=1; while (i < pow) { result = result * n; i++; } } 

代码出了什么问题:

  1. 没有使用有意义的变量名 ,使代码的含义难以理解; 记住代码是为人而不是编译器编写的。
  2. 不要使用这个代码令人困惑的代码int x,a,b,y=0; 令人困惑,所有变量都设置为0或只是y 。 始终将已初始化的变量放在单独的行上。 它使阅读更容易。 加倍努力是明确的,从长远来看,它将带来巨大的回报。
  3. 使用注释:如果你不知道什么是阿姆斯特朗号码,那么从你的代码中很难说出来。 添加一些有意义的评论,以便人们知道它该做什么代码。 这将使您和其他人更容易,因为他们知道您的意图可以看到您实际做了什么,并在需要时解决差异。
  4. 使用有意义的例程名称 WTF fun(x)fun(x)吗? 永远不要说任何fun()事情fun()它就像事实上的自由科学,重点是什么?
  5. 不要硬编码 ,你的例程只接受了armstrong3号,但是如果你可以硬编码那么为什么不做return (input == 153) || (input == 370) || .... return (input == 153) || (input == 370) || ....
 /* Name: Rakesh Kusuma Email Id: rockykusuma@gmail.com Title: Program to Display List of Armstrong Numbers in 'C' Language */ #include #include int main() { int temp,rem, val,max,temp1,count; int num; val=0; num=1; printf("What is the maximum limit of Armstrong Number Required: "); scanf("%d",&max); printf("\nSo the list of Armstrong Numbers Before the number %d are: \n",max); while(num <=max) { count = 0; temp1 = num; while(temp1!=0) { temp1=temp1/10; count++; } if(count<3) count = 3; temp = num; val = 0; while(temp>0) { rem = temp%10; val = val+pow(rem,count); temp = temp/10; } if(val==num) { printf("\n%d", num); } num++; } return 0; } 

检查编号是阿姆斯特朗或不使用C语言

 #include #include void main() { A: int n,n1,rem,ans; clrscr(); printf("\nEnter No. :: "); scanf("%d",&n); n1=n; ans=0; while(n>0) { rem=n%10; ans=ans+(rem*rem*rem); n=n/10; } if(n1==ans) { printf("\n Your Entered No. is Armstrong..."); } else { printf("\n Your Entered No. is not Armstrong..."); } printf("\n\nPress 0 to Continue..."); if(getch()=='0') { goto A; } printf("\n\n\tThank You..."); getch(); }