High-precision Integer (n-ary, n<=10) addition

Source: Internet
Author: User
Tags strlen
I. Overview

A high-precision integer means that the data range is extremely large and cannot be used to unsigned the data stored by long long. Many OJ have relevant exercises (such as a+b upgrade), and for the purpose of deliberately giving great data this algorithm is a common algorithm.
This chapter only studies the addition of numbers within 10 binary (2-in-binary). This chapter will use the explanation method of the example. Second, examples Example 1

Known two 10 binary number A, a, a+b value. Data Range

The data for a,b,50% is not greater than 10100. Input/Output Input 1

201647458466448 116476489143158 Output 1

318123947609606 Analysis

For this data range, it is obvious to use a high-precision arithmetic method.
The process for high-precision addition is as follows. Take an example of the case.
(1) Use char[] to store two large integers and deposit them all in int[];
(2) Use a new int[] to store the number of additions done, and add up all the bits;
(3) Handle rounding.
The process is shown in the following figure.
Realize

 #include <cstdio> #include <cstring> char a1[105], b1[105]; int a[105], b[105], c[1

05];

    int main (void) {FILE *f = fopen ("In.txt", "R");
    memset (A, 0, sizeof (a));
    memset (b, 0, sizeof (b));
    memset (c, 0, sizeof (c));
    FSCANF (F, "%s%s", A1, B1);
    Fclose (f);
    int alen = strlen (a1), Blen = strlen (B1);
    int i; 
    Reverse put number for (i = 0; i < alen; i++) a[alen-i] = a1[i]-' 0 ';

    for (i = 0; i < Blen; i++) b[blen-i] = b1[i]-' 0 ';
    int clen, decade = 0;
        for (Clen = 1; Clen <= Alen | | clen <= blen; clen++) {C[clen] = A[clen] + B[clen] + decade; decade = C[clen]/10; Storage-Standard carry C[clen]%= 10; Delete the standard carry} C[clen] = decade; Handle the highest bit if (c[clen] = = 0) clen--;  Fixed the number of digits of a large integer after rounding//after the above procedure, the elements from 0 to Clen in the C array have stored the numbers, just reverse them out for (i = Clen; I >= 1; i--) {printf ("%d",
    C[i]);
} return 0; }//Note: The above +1 or 1 operations on some key corner marks are to skip the char[] flag end of the \ 
Example 2

Known two n binary number A, B, A+b (n-binary) values are obtained. Data Range

The data for a,b,50% is not greater than 10100. Input/Output Input 1

10
201647458466448 116476489143158 Output 1

318123947609606 Input 2

2
11111111101010100011 11010001101000101011 Output 2

111010001010011001110 Analysis

Change the example 1 implementation in the carry process. Implement

#include <cstdio>
#include <cstring>

char a1[105], b1[105];
int a[105], b[105], c[105];

int main (void) {
    FILE *f = fopen ("In.txt", "R");

    memset (A, 0, sizeof (a));
    memset (b, 0, sizeof (b));
    memset (c, 0, sizeof (c));
    int n;
    FSCANF (F, "%d%s%s", &n, A1, B1);
    Fclose (f);
    int alen = strlen (a1), Blen = strlen (B1);
    int i;
    for (i = 0; i < alen; i++) a[alen-i] = a1[i]-' 0 '; 
    for (i = 0; i < Blen; i++) b[blen-i] = b1[i]-' 0 ';

    int clen, decade = 0;
    for (Clen = 1; Clen <= Alen | | clen <= blen; clen++) {
        C[clen] = A[clen] + B[clen] + decade;
        decade = C[clen]/n; Use n binary 
        C[clen]%= n;
    }

    C[clen] = Decade;
    if (c[clen] = = 0) clen--;

    for (i = Clen; I >= 1; i--) {
        printf ("%d", C[i]);
    } 
    return 0;
}
Example 3

(the subject from the NOIP1999 to improve the group of test questions, you can view this problem in Vijos, the topic is also the official CCF P231 Exercise 5th, Vijos submit record AC)
If a number (first not 0) is read from left to right and read from right to left, we call it a palindrome number.
For example: Given a 10 binary number 56, will 56 plus 56 (that is, 56 right-to-left reading), to get 121 is a palindrome number.
Another example: for 10 binary number 87:
step1:87+78 = 165 step2:165+561 = 726
step3:726+627 = 1353 step4:1353+3531 = 4884
One step here is to make an n-ary addition, and the above example uses at least 4 steps to get the palindrome number 4884.
Write a program, given an N (2<=n<=10 or n=16) into the number of M, where 16 binary number is 0-9 and a-f, the minimum number of steps to get a palindrome. If the palindrome number cannot be obtained within 30 steps (including 30 steps), the output "impossible!" Data Range

2<=n<=10 or N=16
0<=m<=maxlongint input/output input 1

9
Output 1

step=6 Analysis

Due to the uncertainty of the input system and the possible resulting data is too large, you can consider using example 2 of the high-precision operation processing, can make the algorithm more flexible.
Note: To special handle 16 binary. The method used here is to translate the 16 binary into 10, and then translate back to the 16 binary processing. The solution is not optimal. (Remember to comment out the debug output.) ) to achieve

#include <cstdio> #include <cstring> #include <cctype> const int MAXN = 10001;
int A[MAXN], B[MAXN], C1[MAXN];

const char hex[] = "0123456789ABCDEF";
    High-precision addition, returns the number of bits of the result int hplus (int n, char a1[], Char b1[], char c[]) {memset (A, 0, sizeof (a));
    memset (b, 0, sizeof (b));
    memset (c1, 0, sizeof (C1));
    int alen = strlen (a1), Blen = strlen (B1);

    int i; for (i = 0; i < alen; i++) A[alen-i] = IsDigit (A1[i])?
    A1[i]-' 0 ': a1[i]-' A ' + 10; for (i = 0; i < Blen; i++) b[blen-i] = IsDigit (B1[i])?

    B1[i]-' 0 ': b1[i]-' A ' + 10;
    int clen, decade = 0;
        for (Clen = 1; Clen <= Alen | | clen <= blen; clen++) {C1[clen] = A[clen] + B[clen] + decade;
        decade = C1[clen]/n;
        C1[clen] = C1[clen]% n;
    printf ("A%d B%d C1%d\n", A[clen], B[clen], C1[clen]);
    } C1[clen] = decade;

    if (c1[clen] = = 0) clen--;
    int j = 0; for (i = Clen; I >= 1; i--) {//printf ("I%d C1%d\n", I, C1[i]); C[J] = hex[c1[i]];
    16 binary J + +;
} return Clen;  
    } void Rev (char string[], char reved[]) {int len = strlen (string);  
    int i;  
    for (i = 0; i < len; i++) {reved[len-i-1] = string[i];

}} char Input[maxn], REVED[MAXN], TEMP[MAXN];

    int main (void) {//file *f = fopen ("In.txt", "R");
    int n;
    scanf ("%d%s", &n, input);
    Fclose (f);
    int i;
        for (i = 0; I <=; i++) {memset (reved, 0, sizeof (reved));
        Rev (input, reved);
        printf ("Input%s reved%s\n", input, reved);
            if (!strcmp (input, reved)) {printf ("step=%d", I); 
        return 0;
        } hplus (n, input, reved, temp);
    strcpy (input, temp);
    } printf ("impossible!");
return 0; }
Iii. Summary

High-precision addition (in fact, subtraction can also be used in such a way to write) in fact, a simulation of hand-column vertical type of such a work. However, it is important to note that the complex operation of char[] is extremely easy to write and error-prone, and remember to be careful when writing this algorithm.

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.