# Hangzhou Electric HDU1042 (high precision with a bit of pit)

Source: Internet
Author: User

title: http://acm.hdu.edu.cn/showproblem.php?pid=1042

Test instructions

Given an integer N (0≤n≤10000), your task is to calculate n! isn't it easy? General Method:
`#include <iostream>#include<cstring>using namespacestd;Const intMAXN =10000;intA[MAXN];intMain () {intN, K, temp;  while(SCANF ("%d", &n)! =EOF) {memset (A,0,sizeof(a)); a[0]=1;  for(intI=1; i<=n; i++) {k=0;  for(intj=0; j<maxn; J + +) {A[j]=a[j]*i+K; K=a[j]/Ten; A[J]%=Ten; }        }        intT;  for(t=maxn-1; t>=0; t--)        if(A[t]) {//cout<<t<<endl;             Break; }             for(inti=t; i>=0; i--) printf ("%d", A[i]); printf ("\ n"); }    return 0;}`

Is the above code the same as your idea? , unfortunately, the above code will definitely time out! So, can you turn the array smaller? ----> not. When n=10000, you will find that the array is open to 9999. Obviously, this problem is to card your time, is to card your optimization. Here are two ideas for optimization:

1. Consolidation: thus reducing the number of calculations, for example, you save 10000 order of magnitude in each a[i], then the length of this array is 2000! But this algorithm in the implementation to consider a lot of situations, more cumbersome!

2. Process optimization: As the result value in the calculation, the number of changes is very large, but the above code, in the calculation each time according to MAXN-1 bit calculation, so did a lot of useless. This can save a lot of time if you calculate Shidushun with the number of digits. The code can only be changed slightly!

3. Use the first two methods in a comprehensive way!

Because the first and third methods are more cumbersome, I no longer bother!

`#include <iostream>#include<cstdio>using namespacestd;Const intmaxn=100002;intA[MAXN];intMain () {intN; intk,count,temp;  while(SCANF ("%d", &n)! =EOF) {a[0]=1; Count=1;  for(intI=1; i<=n;i++) {k=0;  for(intj=0; j<count;j++) {Temp=a[j]*i+K; A[J]=temp%Ten; K=temp/Ten; }             while(k)//¼çâ¼½øî»{A[count++]=k%Ten; K/=Ten; }        }             for(inti=count-1; i>=0; i--) printf ("%d", A[i]); printf ("\ n"); }    return 0;}`

Time consuming: 1045MS time limit is 5s.

However:

`#include <iostream>#include<cstdio>using namespacestd;Const intMAXN =100000;intMain () {intN, A[MAXN]; intI, J, K, Count, temp;  while(cin>>N) {a[0]=1; Count=1;  for(i=1; i<=n; i++) {k=0;  for(j=0; j<=count; J + +) {Temp=a[j]*i+K; A[J]=temp%Ten; K=temp/Ten; }             while(k) {A[count++]=k%Ten; K/=Ten; }        }         for(j=maxn-1; j>=0; j--)        if(A[j]) Break;  for(i=count-1; i>=0; i--) cout<<A[i]; cout<<Endl; }    return 0;}`

Time consuming: 811MS is very confusing! Should CIN and cout be slower than scanf? , but the truth is, it seems that the book is not necessarily correct. Although we all understand this truth, but unconsciously still superstitious authority. The reality is changeable, and there will be unexpected results in the face of different situations. So never self-righteous, never say too absolute,-------seems to fall into the paradox. Oh!

，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，

Hangzhou Electric HDU1042 (high precision with a bit of pit)

Related Keywords:

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.