NWU CCCC选拔赛 1014阶乘问题(思维题)

 

阶乘问题

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 21   Accepted Submission(s) : 4

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

N的阶乘写作N!,表示小于等于N的所有正整数的乘积。 阶乘会变大得很快,如13!就必须用32位整数类型来存储,到了70!即使用浮点数也存不下了。 你的任务是找到阶乘最前面的非零位。举两个例子:
5!=1*2*3*4*5=120,所以5!的最靠前的非零位是2。
7!=1*2*3*4*5*6*7=5040,所以最靠前的非零位是4。

Input

循环输入多组数据。
每组数据一个整数N(n<=4220)。

Output

每组数据输出N!最靠后的非零位。

Sample Input

7

Sample Output

4

思路:看代码应该明白的,不多讲了。体会体会

#include <iostream>
using namespace std;

int main()
{
    int n;
    while(cin>>n){
        long long ans = 1;
        for(long long i = 1;i <= n;i ++){
            ans *= i;
            while(ans%10 == 0)
                ans /= 10;
            ans = ans%10000;
        }
        cout<<ans%10<<endl;
    }
    return 0;
}


 

posted on 2016-07-07 21:58  Jstyle  阅读(432)  评论(0编辑  收藏  举报

导航