The storage problem of large numbers in C language

In the C language, common data types are short, int and other data types. There is no problem when storing and representing smaller numbers, but when we want to store and represent large numbers, data will appear. For the overflow problem, take the factorial as an example. If you use int data, the factorial data to 13 will overflow. The result is 1932053504 (using Codeblocks), which is obviously wrong.
So how to solve this problem? One of the key tools we consider to use here is the array. The basic idea is to assign the number of each digit obtained during the factorial process to the elements of the array. If an element in the array appears to be greater than the factorial process For a number equal to 10, take its carry and place it to the previous one (due to the reverse order output, that is, the last one of the array element), the local element is updated to the carry of the ones place of the current element and the previous one of the array elements. In this process, there is a problem of the highest order that needs to be solved, that is, the starting point of the output. The highest order is 0 by default (the array subscript starts from 0). When the highest order array element also gets a number greater than or equal to 10. , The operation of the highest bit +1 is also required.
In fact, after reading the basic ideas, I believe that many friends have roughly understood this program, but there may still be problems in how to use code to write in practice. The specific code I wrote is as follows for your reference:

#include<stdio.h>
int main(){
    
    
        int arr[50]={
    
    1};                            //数组的大小可以根据需要设置,这里我按照50来编写的,数组第一位(arr[0])设置为1,其余为0//
        int high=0;                                 //设置最高位//
        int i;                                      //历遍数组的下标//
        int j=1;                                    //阶乘起点//
        int n;                                      //阶乘终点//
        int temp[50];                               //进位的数组//
        int flag;
        scanf("%d",&n);
        while(j<=n){
    
    
            for(i=0;i<=high;i++){
    
    
                arr[i]=arr[i]*j;                     //数组每一位元素与n之前的数相乘//
                    temp[i]=arr[i]/10;               //求每一个数组元素的进位置于temp数组中//
                    if(i==0) arr[i]=arr[i]%10;       //数组元素求本位,本位为本位元素的个位与前一位元素的进位之和//
                    else arr[i]=arr[i]%10+temp[i-1];
                    if(i==high&&temp[i]>0){
    
             //当数组最高位的元素有进位时最高位改变//
                        high=high+1;
                        arr[high]=temp[i];           //最高位元素的更新//
                        break;
                        }
                    else ;

            }
            j++;
        }
//下面的循环将进位后得到的数组元素中所有能继续进位的元素继续进位,进位不彻底是一个很容易犯的错误!!//
        do{
    
    
            flag=0;                                  //每次循环执行时先设置flag变量为0//
            for(i=0;i<50;i++){
    
                           //先将temp数组中元素全部置为0,防止进位后的temp数组不能覆盖掉原来temp数组非0的元素,从而进入死循环//
                temp[i]=0;
                }
            for(i=0;i<=high;i++){
    
    					  //与上同//
                temp[i]=arr[i]/10;
                arr[i]=arr[i]%10+temp[i-1];
                if(i==high&&temp[i]>0){
    
                 
                        high=high+1;
                        arr[high]=temp[i];
                        break;
                }
            }
            for(i=0;i<50;i++){
    
                            //如果进位后的temp数组中还有非0的元素说明进位不彻底,flag赋值为1,继续进位//
                if(temp[i]) flag=1;
                }
        }while(flag);
        printf("%d的阶乘:",n);
        for(i=high;i>=0;i--){
    
    						  //从最高位开始逆序输出//
            printf("%d ",arr[i]);
        }
        printf("\n");
}

I have debugged the code, and there should be no problem. I suggest that you test a few more sets of data when you debug again, because the data you tested may just avoid the bugs in your program.
I hope this article can be helpful to everyone, and if there are problems or simplifications in the code, you are welcome to criticize and correct.
Finally, I would like to encourage everyone and move forward together on the road of learning programming!

Guess you like

Origin blog.csdn.net/qq_52486831/article/details/115044187