Add Large Numbers (C Language)

1+1=2 I believe everyone can count, but when you are faced with a very large number of two digits, can you quickly calculate the answer obtained by adding them together? And now we need to deal with such a problem, using C language to calculate the result obtained by adding two-digit numbers, but not more than 1000 digits.

Example: hdu-1002

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

Why are you writing this blog today?

#include<stdio.h>
#include<string.h>

// Preprocess the value, remove the leading 0 
void prefix( char num[])
{
    if (num[ 0 ] != ' 0 ' )   // no leading 0, no processing 
    {
         return ;
    }
    char *p = num;
     for (; *p == ' 0 ' && *p != 0 ; p++);   // shift p to point to the first non-zero digit 
    if (*p == 0 )
    {
        num[ 0 ] = ' 0 ' ;
        num[ 1 ] = 0 ;
    }
    else
    {
        num[ 0 ] = * p;
        for ( char *q = num; *p != 0 ; *(++q) = *(++ p));
    }
}

int main(int argc,char *argv[])
{
    int flag= 0 ;
     char a[ 1001 ],b[ 1001 ];    // Number memory 
    int ans[ 1001 ]={ 0 };       // And memory 
    int n= 0 ;                 // Number of iterations
    
    // Get the variable 
    scanf( " %d " ,& n);
    
    // Iterate 
    for ( int p= 1 ;p<=n;p++ )
    {
        flag ++ ;
         // read two numbers 
        scanf( " %s%s " ,a,b);
        
        // Remove leading 0 
        prefix(a);
        prefix(b);
        
        // Get the length of two strings 
        int lena= strlen(a);
         int lenb= strlen(b);
        
        // Make pa store a string with a larger length 
        char *pa,* pb;
         if (lena< lenb)
        {
            lena + = lenb;
            lenb = lena- lenb;
            lena -= lenb;
             // The purpose of exchanging lena and lenb is to make the number pointed to by pa longer 
            pa= b;
            pb=a;
        }
        else
        {
            pa = a;
            pb=b;
        }
        
        // start of calculation 
        int upa= 0 ; // carry 
        int r= 0 ; // enumeration to store data array 
        for ( int i=lena,j=lenb;i> 0 ;)
        {
            // Get the current digit of two numbers 
            int na=pa[--i]- ' 0 ' ;
             int nb=j> 0 ? pb[--j]- ' 0 ' : 0 ;    // The digit pointed to by pb Shorter, it may be 0 if there are not enough digits in this calculation
            
            // Get the sum of the current bit 
            int sum=na+nb+ upa;
            
            // Get the carry, such as 9+8=17>10, it is considered that the corresponding bit is 7, and the previous bit is carried by 1 
            upa=sum/ 10 ;
            
            // Store the current bit 
            ans[r++]=sum% 10 ;
        }
        
        // Check if there is a carry in the last bit 
        if (upa> 0 )
        {
             ans[r++]=upa;
        }
        
        // Output Case #: 
        printf( " Case %d:\n " ,flag);
        
        // output a + b = 
         printf( " %s + %s = " ,a,b);
         
         // output result 
         while (r> 0 )
        {
            
            printf("%d",ans[--r]);
        }
        
        // newline 
        if (p!= n)
        {
            printf("\n\n");
        }
        else
        {
             printf("\n");
        }
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324795474&siteId=291194637