The length of the number:
N! is a very large number, the formula is: N! =n* (N-1) * (N-2) ... 2*1. Now you need to know how many (decimal) bits n! have. Enter 1 positive integer n per line. For each n, the number of (decimal) digits of the output n!
Analysis:
The so-called n! (decimal) Number of digits, that is (lgn!) +1, according to the mathematical formula, there are n! =1*2*3 ... (N-1) *n, so lgn!=lg2+lg3......lgn.
#include <stdio.h>#include <math.h>intMainvoid){intN,i;//define positive integer n Double sum;//define the number of n! (decimal) digits while(SCANF ("%d", &n)!=eof) {sum=0.0; for(i=2; i<=n;++i) {sum=sum+LOG10 (i); } printf ("%d\n",(int)sum+1); }return 0;}
Leftmost number
/* gives a positive integer n, enter the leftmost number of n^n. The
input contains a series of measured arrays, the first behavior of the input group, the integer t, representing a total T group, each set of test groups containing a positive integer n,1<=n<=1000000000. The
output n^n the leftmost book.
Analysis:
This topic is primarily given a positive integer n, which computes the value of the number at the highest n^n of the result.
If the final result of the n^n and then the highest number, the method is simple, but there is a problem, that is, N is large, n^n is too large to be stored with the program, need to design a large number of storage scheme, and bring the computational process difficulty
and consume a lot of memory space and time, not advisable.
Another method, set N^n=d. . 10^ (K-1), where k represents the number of bits of n^n, then D. =10^ ((lgn^n)-(k-1)), and then to D. The * takes the whole to get the final result. Because k equals the integer portion of lgn^n plus 1, i.e. k=lgn^n+1 (rounding)
, the formula for D is obtained d=10^ (lgn^n-lg10n^n) (rounding) */
#include <stdio.h> #include <math.h> int Main ( void ) {int n,t; scanf ( "%d" , &t); while (t--) {scanf ( "%d" , &n); printf ( "%d\n" , (int ) (10 , N*log10 (N)-( int ) (N*log10 (N)))); } return 0 ;}
Basic Math problems