[C++] henuACM summer training Day13 high-precision computing

Large integer addition

Int can store ten positive and negative numbers, double is 15 digits, and longlong has only 19 digits. At this time, if you enter a number with hundreds or thousands of digits for calculation, you need to use large integers. The method is to save each number in an array. For example, to save a two-hundred-digit integer, we can use a[200] to save, let a[0] save everybody, a[1] save ten digits...

As for how to add, it is to use the analog column vertical type to do the addition, starting from the ones digit and adding it bit by bit, and if it exceeds or reaches 10, it will be carried out.

Write the general idea code first

#include <stdio.h>
#include <string.h>
#define MAX_LEN 200//二百位以内的大整数
int an1[MAX_LEN+10];//an是用来保存输入的字符转换成的数字
int an2[MAX_LEN+10];
char szLine1[MAX_LEN+10];//szline是用来保存输入的字符
char szLine2[MAX_LEN+10];
int main(void)//这个void是main函数不需要传入参数的标准写法,在Linux下都是这样写的
{
    
    
    scanf("%s", szLine1);
    scanf("%s", szLine2);
    int i, j;
    memset( an1, 0, sizeof(an1));
    memset( an2, 0, sizeof(an2));
int nLen1 = strlen( szLine1);
    for( j = 0, i = nLen1 - 1;i >= 0 ; i --)
        an1[j++] = szLine1[i] - '0';//输入的数字字符减去字符0,就可以得到字符对应的数字
    int nLen2 = strlen(szLine2);
    for( j = 0, i = nLen2 - 1;i >= 0 ; i --)
        an2[j++] = szLine2[i] - '0';
    for( i = 0;i < MAX_LEN ; i ++ )
    {
    
      an1[i] += an2[i]; //逐位相加
        if( an1[i] >= 10 )
        {
    
     //看是否要进位
            an1[i] -= 10;//进位后要减去10
            an1[i+1] ++; //进位
        }
    }
    for( i = MAX_LEN; (i >= 0) && (an1[i] == 0); i -- ) ;//这里其实还没有搞懂
    if(i>=0)
        for( ; i >= 0; i--)
            printf("%d", an1[i]);
    else      printf("0");
    return 0;
}

The above is the code to add two big numbers. In the next freshman practice competition, a group of data is added, which will be explained when the problem is solved in that lesson.

Large integer subtraction

When I was a child, I learned addition and subtraction. In fact, subtraction can be said to be just an upgrade of addition, because there will be positive and negative problems. In large integer subtraction, one variable is used to store the sign bit, and another array stores the difference. Absolute value.

The following code snippet represents the storage of the sign bit. In order to show clearly what AB is, the definition in front of the main function is also copied.

int main()
{
    
    
    char A[maxn];
    char B[maxn];
    scanf("%s%s",&A,&B);
    if(strlen(A)<strlen(B)||(strlen(A)==strlen(B)&&strcmp(A,B)<0))
    {
    
    
        swap(A,B);//交换
        printf("-");
    }

The same as the addition of large integers, the input is all characters instead of numbers, so two arrays must be defined to store the numbers minus 0.

In this subtraction operation, since it is not as intuitive as addition, the senior student newly defined an array c to store the result.

The biggest problem in subtraction is the leading zero, such as 123-120 the answer is 3, not 003. This is the code to remove the leading zeros.

 if(c[num]==0){
    
    num--;}
        for(int i=num;i>=0;--i){
    
    
            cout<<c[i];
        }

Where num is the number of bits representing c

The complete code is as follows:

#include <iostream>
#include <string.h>
using namespace std;
const int maxx = 10005;
int main()
{
    
    
    char A[205],B[205];
    cin>>A>>B;
    if(strlen(A)<strlen(B) || (strlen(A)==strlen(B) && strcmp(A,B)<0) )
    {
    
    
        swap(A,B);
        cout<<"-";
    }
    int a[205],b[205];
    int lena = strlen(A);
    int lenb = strlen(B);
    for(int i=0;i<lena;++i){
    
    
        a[i] = A[lena-i-1]-'0';
    }
    for(int i=0;i<lenb;++i){
    
    
        b[i] = B[lenb-i-1]-'0';
    }
    int c[205];
    int num=0;
    for(int i=0,g=0;i<lena;++i){
    
    
        int tmp = a[i]-g;
        if(i<lenb){
    
    
            tmp =tmp-b[i];
        }
        if(tmp>=0){
    
    
            g=0;
        }
        else{
    
    
            g=1;
            tmp = tmp+10;
        }
        c[num]=tmp;
        num++;
    }
    num = num-1;
    if(c[num]==0){
    
    num--;}
        for(int i=num;i>=0;--i){
    
    
            cout<<c[i];
        }

    return 0;
}

Guess you like

Origin blog.csdn.net/qq_44899247/article/details/97622808