编写一个C程序,它接受两个数字,并找到该范围内的所有阿姆斯特朗数

xriantvc  于 2022-12-17  发布在  其他
关注(0)|答案(2)|浏览(152)

在上面提到的我想问我做错了什么在我的代码我已经尝试调试它很多次,但无法理解的逻辑错误在我的代码。
任何帮助都将不胜感激。

#include <stdio.h>
#include <math.h>

int digit(int n);

int digit(int n) {
    int a;
    double i = 0;
    do {
        a = n % (int)(pow(10, i));
        i++;
    } while (a != n);
    return i;
}

void is_armstrong(int n);

void is_armstrong(int n) {
    int a, b;
    double sum;
    for (int i = 0; i < digit(n); i++) {
        a = n / (int)pow(10, (double)i);
        b = a % 10;
        sum += pow((double)b, 3);
    }
    if ((int)sum == n) {
        printf("%d is an armstrong number.\n", n);
    }
}

int main() {
    int a, b;
    printf("Please input the left hand limit of range : \n");
    scanf(" %d", &a);
    printf("Please input the right hand limit of range : \n");
    scanf(" %d", &b);
    for (int i = a; i <= b; i++) {
        is_armstrong(i);
    }
    is_armstrong(153);
    return 0;
}

这个代码甚至没有显示阿姆斯特朗的号码153。

ktecyv1j

ktecyv1j1#

不需要计算n中的位数,您只需对每个位数的立方求和,每次迭代时一个立方除以10。
以下是一个简化版本:

#include <stdio.h>

void is_armstrong(int n) {
    int sum = n;

    while (n != 0) {
        int b = n % 10;
        n /= 10;
        sum -= b * b * b;
    }
    return sum == 0;
}

int main() {
    int a, b;

    printf("Please input the left hand limit of range:\n");
    if (scanf("%d", &a) != 1)
        return 1;
    printf("Please input the right hand limit of range:\n");
    if (scanf("%d", &b) != 1)
        return 1;

    for (int i = a; i <= b; i++) {
        if (is_armstrong(i)) {
            printf("%d is an Armstrong number.\n", i);
        }
    }
    return 0;
}
kx1ctssn

kx1ctssn2#

注意到关于使用幂函数的评论以及在给定范围内识别阿姆斯特朗数的最终结果,我做了一些重构来简化识别这些数的过程。

#include <stdio.h>

void is_armstrong(int n) {
    int a, b, c, d;
    int sum = 0;

    a = n;
    c = 0;

    while (a != 0)              /* Determine the number of digits to raise to a power */
    {
        a = a / 10;
        c = c + 1;
    }

    a = n;                      /* Reset the work number */

    while (a != 0)              /* Noted from the comments to simplify the test */
    {
        b = a % 10;
        d = b;

        for (int i = 1; i < c; i++)
        {
            d = d * b;
        }

        sum = sum + d;          /* Just mulitply each digit by itself the required number of times */
        a = a / 10;             /* Divide by 10 along with using the modulo function to evaluate each digit */
    }

    if (sum == n) {
        printf("%d is an armstrong number.\n", n);
    }
}

int main() {
    int a, b;

    printf("Please input the left hand limit of range : \n");
    scanf(" %d", &a);
    printf("Please input the right hand limit of range : \n");
    scanf(" %d", &b);

    for (int i = a; i <= b; i++) {
        is_armstrong(i);
    }

    return 0;
}

以下是一些要点。

  • 由于不需要幂函数,因此不需要math. h include文件,也不需要链接数学库。
  • 通过仅利用与整数除以“10”相结合的模运算来简化获取每个数字。
  • 获取每个数字的n次幂的值的过程被简化,只需执行一次重复的乘法。

以下是端子的测试输出。

@Dev:~/C_Programs/Console/Armstrong/bin/Release$ ./Armstrong 
Please input the left hand limit of range : 
1
Please input the right hand limit of range : 
10000
1 is an armstrong number.
2 is an armstrong number.
3 is an armstrong number.
4 is an armstrong number.
5 is an armstrong number.
6 is an armstrong number.
7 is an armstrong number.
8 is an armstrong number.
9 is an armstrong number.
153 is an armstrong number.
370 is an armstrong number.
371 is an armstrong number.
407 is an armstrong number.
1634 is an armstrong number.
8208 is an armstrong number.
9474 is an armstrong number.

并且作为确认,可以看出值“153”被识别为阿姆斯特朗数。
给予代码片段,看看它是否符合项目的精神。

相关问题