hoj 1035 factorial

来源:互联网 发布:华夫饼的配方 知乎 编辑:程序博客网 时间:2024/06/04 14:26

先把题弄过来:

The most important part of a GSM network is so calledBase Transceiver Station (BTS). These transceivers form theareas calledcells (this term gave the name to the cellular phone)and every phone connects to the BTS with the strongest signal (ina little simplified view). Of course, BTSes need some attention andtechnicians need to check their function periodically.

ACM technicians faced a very interesting problem recently. Given a set ofBTSes to visit, they needed to find the shortest path to visit all of thegiven points and return back to the central company building. Programmershave spent several months studying this problem but with no results. Theywere unable to find the solution fast enough. After a long time, one of theprogrammers found this problem in a conference article. Unfortunately, hefound that the problem is so called "Travelling Salesman Problem" and it isvery hard to solve. If we have N BTSes to be visited, we can visit them inany order, giving usN! possibilities to examine. The function expressingthat number is called factorial and can be computed as a product1.2.3.4....N. The number is very high even for a relatively smallN.

The programmers understood they had no chance to solve the problem. Butbecause they have already received the research grant from the government,they needed to continue with their studies and produce at leastsomeresults. So they started to study behaviour of the factorial function.

For example, they defined the function Z. For any positive integer N,Z(N) is the number of zeros at the end of the decimal form of numberN!. They noticed that this function never decreases. If we have two numbersN1<N2, thenZ(N1) <= Z(N2). It is because we can never "lose" anytrailing zero by multiplying by any positive number. We can only get newand new zeros. The functionZ is very interesting, so we need a computerprogram that can determine its value efficiently.

Input Specification

There is a single positive integer T on the first line of input. It standsfor the number of numbers to follow. Then there isT lines, each containingexactly one positive integer number N, 1 <=N <= 1000000000.

Output Specification

For every number N, output a single line containing the single non-negativeintegerZ(N).

Sample Input
63601001024234568735373
Sample Output
0142425358612183837
感觉自己笨笨的,不会做。

拿到这个题首先想到的的确也是与5有关的,可是再怎么弄就不知道了。

后来就又想着重蹈覆辙,阶乘再取后几位,不断模10,这个小数还可以,8735373这个就通不过,毕竟有一定的限制。然后,我又网上搜答案。

真相原来是:不断的除以5,直到0为止,期间记录商的和。

顿时凌乱啊。唔,笨啊。。。。

code:

#include <iostream>using namespace std;int main(){    int n ;     cin >> n ;     int numbers[n] ;    int zeros[n] ;      for(int i = 0 ; i < n ; i++)    {         cin >> numbers[i] ;          zeros[i] = 0 ;          while(numbers[i])         {              zeros[i]+=numbers[i]/5 ;               numbers[i]/=5 ;              }                       }    for(int i = 0 ; i < n ; i++)    {         cout <<zeros[i] <<endl ;     }    return 0;}


原创粉丝点击