一道ACM的简单题~

来源:百度知道 编辑:UC知道 时间:2024/05/21 19:25:12
题目:
Description
The expression N!, read as "N factorial," denotes the product of the first N positive integers, where N is nonnegative. So, for example,

N N!
0 1
1 1
2 2
3 6
4 24
5 120
10 3628800

For this problem, you are to write a program that can compute the last non-zero digit of any factorial for (0 <= N <= 10000). For example, if your program is asked to compute the last nonzero digit of 5!, your program should produce "2" because 5! = 120, and 2 is the last nonzero digit of 120.

Input
Input to the program is a series of nonnegative integers not exceeding 10000, each on its own line with no other letters, digits or spaces. For each integer N, you should read the value and compute the last nonzero digit of N!.

Output
For each integer input, the program should print exactly one line of out

我的想法是,让i作为循环变量从1到n运算,用一个整形变量now来存储当前计算结果的最后一位,然后与当前的i相乘,然后舍去后面所有的非0位,继续下去……
now:=1;
for i:=1 to n do
begin
now:=now*i;
while (now mod 10)=0 do
now:=now div 10; //舍去后面的0
now:=now mod 10; //只取最后一位
end;
write(now);
不好意思,我是搞oi的,还只会pascal语言,时间仓促,算法有可能会与一些错误,见谅