【问题标题】:not getting a desired output in armstrong number在阿姆斯壮数中没有得到想要的输出
【发布时间】:2022-02-28 02:39:14
【问题描述】:

它是一个在所需范围内查找阿姆斯壮数的程序 假设我想在 100 到 1000 之间找到 amstrong 然后它显示输出为 370 371 407 但不是 153 找不到问题所在

#include<stdio.h>
#include<math.h>
int main()
{
int num,start,end,n,rem,res;
printf("Enter the range :");
scanf("%d %d",&start,&end);
printf("Amstrong number between %d and %d are : ",start,end);
for(int i=start;i<=end;i++)
{
    //int i=153;
    num=i;
    n=0;
    while(num)
    {
        num/=10;
        ++n;
    }
    num=i;
    res=0;
    while(num)
    {
        rem=num%10;
        res=res+pow(rem,n);
        num/=10;
    }
    if(res==i)
        printf("%d\t",i);
}
return 0;
}

【问题讨论】:

  • 我认为pow() 函数有问题。我在代码中编写了另一个 power() 函数,它运行良好,所以 pow() 函数出了什么问题
  • pow 函数接受doubles 并返回double。您可能会遇到舍入错误,并且 if (res==i) 不正确。
  • 我在 linux 18.04 和 gcc 下运行了 OPs 代码。结果是正确的 4 个输出。我怀疑你的问题是使用pow() 函数

标签: c


【解决方案1】:

我尝试在 Hackerrank 编辑器上运行您的代码,它工作得非常好,并且也打印了 153。

试试看。还附上截图:

【讨论】:

  • 所以我认为它的编译器有问题,因为它在 dev c++ 上运行良好,但在 code:blocks 上却没有。
【解决方案2】:
#include<stdio.h>
#include<math.h>

void main() {
  int number, i, digit, sum, start, end;
  printf("Enter the range :");
  scanf("%d %d", &start, &end);
  for (i = start; i <= end; i++) {
    number = i;
    sum = 0;

    while (number > 0) {
        digit = number % 10;
        sum += digit * digit * digit;
        number /= 10;
    }

    if (sum == i) {
        printf("%d\t", i);
    }
}

}

试试这个

【讨论】:

  • 他需要一个整数幂函数。 Harding 将乘法编码为 3 的幂会破坏他的应用程序的其余部分。
【解决方案3】:

用户 KamilCuk 似乎有正确的想法。

请原谅使用 c++ 而不是 c,但是,您的意外输出(或在这种情况下缺少输出)的原因基本相同。

我已经测试了它的具体值 407 而不是 153,因为这是对我来说不能正常工作的 armstrong 数字(尽管这可能会有所不同)。

    void checkArm()
        {
            cout << "Armstrong numbers in range are:" << endl;
            for (int i = 407; i <= 407; i++)
            { //isolate digits
                int digHun, digTen, digOne;
                digHun = i / 100;
                digTen = i / 10 - 10 * digHun;
                digOne = i % 10;
                cout << "Test 1: " << i << endl;
    
        // the pow function itself provides expected output on terminal
                cout << pow(digHun, 3) << endl;
                cout << pow(digTen, 3) << endl;
                cout << pow(digOne, 3) << endl;
                
    //here's the problem!
    
                cout << "bool check supplied: " << ((pow(digHun, 3) + pow(digTen, 3) + pow(digOne, 3)) == i) << endl;
                cout << "bool check 407: " << ((pow(digHun, 3) + pow(digTen, 3) + pow(digOne, 3)) == 407) << endl;
                cout << "bool check typecasted: " << (int(pow(digHun, 3) + pow(digTen, 3) + pow(digOne, 3)) == i) << endl;
        
//part that was supposed to print 407 if it weren't for problem described above
                if ((pow(digHun, 3) + pow(digTen, 3) + pow(digOne, 3)) == i)
                {
                    cout << "Main test" << i << endl;
                }
            }
        }

我的编译器的输出是:

bool check supplied: 0
bool check 407: 0
bool check typecasted: 1

请注意,仅当我们的表达式在比较之前被强制转换为 int 类型时,条件才成立。只需将此表达式转换为 int 类型即可完成工作。

本质上,对于这两个程序,问题都与 pow 函数有关。尽管 c 和 cpp 之间存在差异,但 pow() 对两者都返回一个 double 值。 此外,传递给 pow() 的参数在执行前被隐式类型转换为 double。

(举个例子,你的编译器将 153 转换为 153.00000etc,然后将其与 153.00023etc 进行比较,它是 pow() 函数的输出。在双重比较的情况下,这些结果并不相等. 实际上,您可以使用 setprecision() 函数来验证这一点。这将产生编译器在比较期间看到的内容)

您可以验证 pow() 确实返回了一个双精度值 here

我希望这能从概念的角度澄清问题(即使语言不同)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 2021-04-03
    • 2022-12-18
    • 2021-04-06
    相关资源
    最近更新 更多