Just the facts ultraviolet A 568

Source: Internet
Author: User

Let's talk about: the question is to find the given n! The first non-zero number of (n <= 10000. Imagine 10000! It is terrible, so my first thought is ~ 10000 of the results are obtained and saved in the array, and then called directly. The method for calculating the result is to start from 1 and save only the last non-zero two digits of the result. In many cases, the first non-zero number has nothing to do with the number of the rear. I tested it according to this idea and found that there is no problem with a small number. When N gradually increases, it will be totally messy. The reason should be that when the temporary result is multiplied by the new N, the first two digits are 0, so the final result is actually related to the third digit of the temporary result, so the result should be kept at least three digits. However, the three digits still have problems. In the end, we have to keep five digits before the water passes. The specific number of digits will appear consecutively and then generate four zeros. I don't know. Hope someone knows this! (* ^__ ^ *) Xi ......



Question:

Just the facts

The expression N !, Read as ''n factorial, "denotes the product of the First n positive integers, where n is nonnegative. So, forexample, expression n! Read as the key multiplication of N, which indicates the multiplication of the First n positive integers. Therefore, for example:

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

For this problem, you are writing a program that can compute the last non-zero digit of any factorial for ().

Now you have to write a program to calculate (0 <= n <= 10000) n! The first non-zero digit.

Example, if your program is asked to compute the last nonzero digit of 5 !, Your program shocould produce''2"Because 5! = 120, and 2 is

For example, calculate 5! The first non-zero number of, your program should output "2" Because 5! = 120, and 2 is the first non-zero number of 120

The last nonzero digit of 120.

Inputinput to the program is a series of nonnegative integers not exceeding 10000, each on its own line with no other letters, digits or spaces.

The input of the program is a series of non-negative integers up to 10000. Each integer has a row and no extra letters, numbers, or spaces.

For each integer N, you should read the value and compute the last nonzero digit of N !.

For each integer N, you need to read and calculate n! The first non-zero digit

Outputfor each integer input, the program shocould print exactly one line of output. Each line of output shoshould contain the value n, right-justified

For each input integer, the program needs to output a row. Each output line contains N, ranging from 1 ~ Right alignment within the range of five columns. If there are insufficient columns, fill them with spaces.

In columns 1 through 5 with leading blanks, not leading zeroes. columns 6-9 must contain''->"(Space hyphen greater space ).

6th ~ 9 columns output "->"

Column 10 must contain the single last non-zero digit of N !.

Output N in row 10! The first non-zero digit

Sample Input
122612531259999

Sample output
    1 -> 1    2 -> 2   26 -> 4  125 -> 8 3125 -> 2 9999 -> 8


Source code:

# Include <stdio. h> # define maxn 10000 + 5 char ans [maxn]; void produce_ans (void); // obtain 0 ~ Int main () {int N; // freopen ("input.txt", "r", stdin); produce_ans (); While (~ Scanf ("% d", & N) printf ("% 5D-> % d \ n", N, ANS [N]); Return 0;} void produce_ans () {int I, temp; ans [0] = 1; temp = 1; for (I = 1; I <= maxn; I ++) {temp * = I; while (TEMP % 10 = 0) temp/= 10; temp = TEMP % 100000; // Save the 5-digit ans [I] = TEMP % 10 ;}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.