这是微软的一道题目,如下:
1、给定应该整数N,那么N的阶乘N!末尾有多少个0?例如:N=10,N!=3628800,N!的末尾有两个0
2、求N!的二进制表示中最低位1的位置
开始我编程如下:
void main(){
_int64 seed=1;
_int64 cnt=0;
_int64 sum;
puts("the number:");
scanf("%I64d",&cnt);
while(cnt>0) seed*=cnt--;
printf("The factorial is:%I64d/n",seed);
sum=seed;
while(seed%10==0) {cnt++;seed/=10;}
printf("The number of /"0/" hehand the factorial is:%I64d/n",cnt);
for(cnt=1;sum%2==0;cnt++,sum/=2);
printf("The first number /"1/" is placed at:%I64d/n",cnt);
}//main
这样虽然没问题,但数字太大会溢出。很显然这样不太好。
于是换了种思想,编程如下:
scanf("%d",&seed);
for(cnt=1;cnt*5<=seed;cnt++);
printf("the number of /"0/" is:%d/n",cnt-1);
for(cnt=0,i=2;i<=seed;i+=2){
for(j=i;j%2==0;cnt++,j/=2);
}//for
printf("The number of /"0/" in the bi-num is:%d/n",cnt);
}//main
真的是思想最重要啊~~以后还要多多努力才对,很久不编程手都生了……