gpt4 book ai didi

c - 阶乘分解的错误输出

转载 作者:行者123 更新时间:2023-12-02 07:02:43 25 4
gpt4 key购买 nike

我做了一个阶乘分解的delphi程序,它看起来像这样:

enter image description here

40 是数字,当您单击“Scomponi”时,您将得到正确的输出 (2^3 * 5 = 40)。我已经制作了一个应该做同样事情的 C 程序,但我有这个输出:

enter image description here

如您所见,右边的数字是正确的 (2^3 * 5),但左边的数字不正确。这是我写的代码:

int main()
{
long a,b=2;
printf("------------------------------------------------ \n \n");
printf("Inserisci il numero di cui vuoi la scomposizione \n"); //input number (it's the 40 of the example)
scanf("%d", &a);
printf("\n------------------------------------------------ \n\n");
printf("Scomposizione: \n \n"); //Decomposition
while(a>1)
{
if(a%b == 0)
{
printf("%d \t \t | %d \n",a,b);
a=a/b;
}
else
{
b++;
}
printf("%d", a);
}
printf("\n------------------------------------------------ \n\n");
getch();
return 0;
}

我该怎么做才能解决这个问题?

最佳答案

   else
{
b++;
}
printf("%d", a);
}

最后的printf不应该在这里。将其删除。

作为旁注:

ablong 类型。

所以代替:

scanf("%d", &a);

你必须使用:

scanf("%ld", &a);

同样适用于printf,使用%ld 转换规范而不是%d

关于c - 阶乘分解的错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17662986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com