You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地编译Armstrong数程序遗漏153且启动缓慢问题求助

Armstrong数查找程序的两个问题

问题详情

  • Armstrong数遗漏:执行以下命令运行程序后,输入上限1000,输出结果遗漏了153(正确列表应包含该数)。
    执行命令:
    PS C:\Users\Paradox\Documents\Prog\C> cd "c:\Users\Paradox\Documents\Prog\C\" ; if ($?) { gcc armstrong.c -o armstrong } ; if ($?) { .\armstrong }
    
  • 启动耗时异常:程序启动后,打印第一个提示语句需要35秒,但后续结果输出瞬间完成。

已尝试的修复

使用round(pow(r,i))替代原代码中的pow(r,i),可以解决153遗漏的问题,但启动耗时问题仍未解决。

程序源代码

#include <stdio.h>
#include <math.h>
int main()
{
    int n1, a, n2, i, r;
    printf("Enter the upper limit for the list of Armstrong Numbers : ");
    scanf("%d", &n1);
    printf("All-Armstrong number between 1 and %d are: ", n1);
    for (int k = 1; k <= n1; k++)
    {
        a = 0,n2 = k,i=0;
        
        do
        {
            n2/= 10;
            i++;
        } 
        while (n2!= 0);
       
        n2 = k;

        do
        {
            r = n2 % 10;
            a+=pow(r, i);
            n2/= 10;
        } 
        while (n2!= 0);

        if (a == k)
        {
            printf("%d ", a);
        }
    }
}

内容的提问来源于stack exchange,提问作者Paradox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 01:42:36