作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我写的程序。当我执行它时,我得到一个空白输出。无法弄清楚它出了什么问题。
#include <stdio.h>
void main() {
int a, b = 0, s, n;
printf("The armstrong numbers are-");
for (n = 1; n <= 10000; n++) {
s = n;
while (n > 0) {
a = n % 10;
b = b + a * a * a;
n = n / 10;
}
if (b == s)
printf("%d ", s);
}
}
最佳答案
正如其他人建议的那样,不要更改 n
在 for 循环内,因为循环取决于变量 n
。你必须设置b
返回0
对于每次迭代。
您的程序可读性不是很好,因为其他人可能不明白a
的含义。 , b
, n
和s
意思是。因此,始终使用有意义的变量名称,如下所示:(有关更多说明,请参阅注释)
#include<stdio.h>
int main(void) //correct signature for main function
{
int digit; //instead of a
int sum=0; //instead of b
int number; //instead of n
printf("The armstrong numbers are-");
for(number = 1; number <= 10000; number++)
{
int temporary = number; //temporary integer to store number value
sum = 0; //sum must be reset to 0 at the start of each iteration
while(temporary > 0)
{
digit = temporary % 10;
sum = sum + (digit * digit * digit);
temporary = temporary / 10;
}
if(sum == number) //if sum obtained == number, print it!
printf("%d ",number);
}
return 0;
}
输出:
The armstrong numbers are-1 153 370 371 407
关于c - 用 C 语言编写一个程序,打印 1 到 10000 之间的阿姆斯特朗数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39951803/
有更有效的方法吗?给定数字 N - 找到所有 list = new LinkedList<>(); for(int i=1; i0) { r = s%
我是一名优秀的程序员,十分优秀!