Big Data factorial

Source: Internet
Author: User

A headache program; Calculate a factorial of less than 100.

This is my program, which can calculate a factorial of less than 10000:

 1 #include<stdio.h> 2  3 void fac(int val){ 4 int i,n,c; 5         int k; 6         int t[1000]={1}; 7         k=0; 8         for(n=1;val>1;val--){ 9             for(c=0,i=0;i<n;i++){10                 k=t[i]*val+c;11                 t[i]=k%10000;12                 c=k/10000;13             }14             t[i]=c;15             if(t[i]>0)n++;    16         }17         printf("%d",t[--n]);18         for(n--;n>=0;n--)19             printf("%.4d",t[n]);20         printf("\n");    21 }22 int main(){23         int a,n,i;24         scanf("%d",&n);25         for(i=1;i<=n;i++){26             scanf("%d",&a);27             fac(a);28         }29     }

Although the results are correct, I still couldn't do it for a long time by referring to other people's algorithms. I finally breathed a sigh of relief.

It wasn't until I saw this program that my code was a bunch of shit.

Excellent code:

 1 /*Small factorials*/ 2   3 #include<stdio.h> 4   5 void fact(short int); 6 void print(void); 7   8 short int factorial[80]; 9 short int length;10  11 int main()12 {13     short int i,n,t;14     scanf("%d",&t);15     for(i=1;i<=t;i++)16     {17         scanf("%d",&n);18         fact(n);19         print();20     }21     return 0;22 }23  24 void fact(short int n)25 {26     short int i,j,sum,temp;27     for(i=1;i<80;i++)28         factorial[i]=0;29     factorial[0]=1;30     length=1;31     for(i=2;i<=n;i++)32     {33         j=temp=0;34         while(j<length)35         {36             sum=temp+factorial[j]*i;37             factorial[j]=sum%100;38             j++;39             temp=sum/100;40         }41         while(temp>0)42         {43             factorial[j++]=temp%100;44             temp/=100;45             length++;46         }47     }48 }49  50 void print(void)51 {52     short int i;53     printf("%d",factorial[length-1]);54     for(i=length-2;i>=0;i--)55     {56         if(factorial[i]>=10)57             printf("%d",factorial[i]);58         else59             printf("0%d",factorial[i]);60     }61     printf("\n");62 } 

1. In terms of format, the latter is clear, the declaration of the function, the definition of the variable, and the blank line of the empty line. The only thing I do is the indent of the indentation.

2. In variable naming, the latter uses simple but clear English. words should be used, and I am a bunch of I, J, K, L, and C; I can't understand it at all.

3. The above two points directly determine the readability and ease of coding. The latter is an excellent code, and I just learned to draw in kindergarten.

4. In terms of content, the latter's code structure is rigorous, and every variable is taken into account. It flexibly applies functions, global variables, and other simple foundations, with clear thinking. The profile has a standard style. I think it's just a beginner.

5. As a result, the latter strictly follows the requirement of the question. 100 is 100, which indicates its strong memory control capability. I am casual, not strict.

To sum up, my code style and thoughts need to be reshaped.

Related Article

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.