回 帖 发 新 帖 刷新版面

主题:1000!尾0问题

[em18]大哥大姐,请快来帮帮我,
急急……
谢谢!
提示: 1000!=1*2*3*……*1000
问:1000!的末尾有多少个“0”?

回复列表 (共24个回复)

11 楼


#include <stdio.h>
int main()
{
    long n,m;
    long num;
    while((scanf("%ld",&n))!=EOF){
        num=0;
        while(n>0){
            m=n;
            while(m%5==0){
                num++;
                m/=5;
            }
            n--;
        }
        printf("%ld\n",num);        
    }
    return 0;
}

12 楼

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    long n = 1000;
    int s = 0;
    while( ( n = n / 5 ) > 0 )
        s += n;
    cout << s;
    getch();
    return 0;
}
        

13 楼

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    long n = 1000;
    int s = 0;
    while( ( n = n / 5 ) > 0 )
        s += n;
    cout << s;
    getch();
    return 0;
}
        

14 楼

有什么难的吗?
好好学数学去
trunc(1000/4)-1

15 楼

每次乘以下一个数后 为a
  if a mod 10 <>0 then
    a:=a mod 10

16 楼

for i:=1 to 1000 do begin
  begin
     s:=s+i;
     if s mod 10 <>0 then s:=s mod 10
     else s:=s div 10
  end

17 楼

不好意思我看错题目了

18 楼

我的算法:
找出能被5整除的+,与偶数相乘有1个0
能被25整除的+2,              2个0
能被125整除的+3               3个0
能被625整除的+4               4个0
注意以上的数只能同时满足一个,否则就重复计算了,最后的结果是249个
#include <stdio.h>
#include <stdlib.h>

void main()
{
    int i = 1 , count = 0 ;
    for( i = 1  ; i <= 1000 ; i++  )
    {
        if( i%625 == 0 )
        {
            count += 4 ;
            printf("%d ",i) ;
        }
        else if(  i % 125 == 0 )
        {
            if( i%625 == 0 )
            {
                continue ;
            }

            else
            {
                count += 3 ;
            }
        }

        else if( i % 25 == 0 )
        {
            if(( i%125 == 0 )||( i%625==0 ))
            {
                continue ;
            }
            else
            {
                count += 2 ;
            }

        }
        else if( i % 5 == 0 )
        {
            if(( i%125 == 0 )||( i%625==0 )||( i%25 == 0 ))
            {
                continue ;
            }
            else
            {
                count ++ ;
            }
        }
    }

    printf("%d",count) ;
    getch() ;
}

19 楼

http://www.programfan.com/blog/article.asp?id=3581

20 楼

好象是
#include <iostream.h>
main()
{
    long n,i;
    while(cin>>n)
    {
        i=(n-(n%5))/5+(n-(n%25))/25+(n-(n%125))/125+(n-(n%625))/625+
        (n-(n%3125))/3125+(n-(n%15625))/15625+(n-(n%78125))/78125+
        (n-(n%390625))/390625;
        cout<<i<<"\n";
    }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册