yuankexinchris 2021-10-06 21:41 采纳率: 100%
浏览 15
已结题

带入一个比较大的数就不对了,比如17的10次方就不对了,但小一点却是对的,这是为啥呢?

输入一个数的n次方,输入后三位
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{ unsigned long a,c, b,n,m=0;
unsigned long s;
scanf("%ld %ld",&a,&n);
s=a ;
if (n==0)
{

printf("The last 3 numbers is 1.\n");
return 0;
}
if (n==1)
{
    printf("The last 3 numbers is %ld.\n",a);
    return 0;
}
if (n>1)

do
{
s=s*a;
m=m+1;
}
while(m<n-1);
b=s;
if (b<1000)
{
c=b;
}
else
{
c=b%1000;
}
printf("The last 3 numbers is %ld.",c);

}
(才学计算机,求大佬们教教,谢谢😊)

  • 写回答

1条回答 默认 最新

  • wr456wr 2021-10-06 22:16
    关注

    额,如果是a赋值为17^10,那么s也是17^10,你后面循环体又有个s=s*a,那么相乘后的s就超unginal long的范围了。(相乘后s有25位,unsigned long范围为20位)🧐

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月15日
  • 已采纳回答 10月7日
  • 创建了问题 10月6日