High accuracy algorithm--step, pressure position and its application

Source: Internet
Author: User

When writing a program, it is often necessary to deal with some big data. We know, however, that the maximum number of integers that can be stored in C + + is \ (2^64-1\). When the big data to be processed exceeds this range, it is necessary to use the so-called high-precision algorithm, the principle is actually the simulation of the vertical calculation (special, Division requires trial quotient).

When actually writing, we will find that a one-time operation takes a lot of hours, so consider how to improve the efficiency, where the use of \ (bitset\) used in the pressure-level thought, that is, 4-bit or 8-bit storage, operation. The most common is to press 4 bits so that multiplication does not overflow. But in this case, a trial business is too slow to divide, so a two-point trial is needed.

#include <algorithm> #include <cstring> #include <cstdio>const int MAXL = 100;struct bign {static        const int BASE = 10000;        static const int WIDTH = 4;        int Len, V[MAXL];                Bign () {len = 0;        memset (V, 0, sizeof (v));  } bign operator = (unsigned long long n) {while (n > 0) {V[++len]                        = n% BASE;                n/= BASE;        } return *this;                } bign operator = (char *s) {int tmp = 0, x = 1;                        for (int i = strlen (s)-1; I >= 0; i--) {tmp + = x * (S[i]-48);                        x *= 10;                                if (x = = BASE) {V[++len] = tmp;                                TMP = 0;                        x = 1; }} if (TMP) {v[++len] = tmp;        } return *this;                } inline void Read () {char s[maxl << 2];                scanf ("%s", s);                *this = s;        Return                } inline void Write () {printf ("%d\n", V[len]);                for (int i = len; I >= 1; i--) printf ("%04d", V[i]);        Return }};

High accuracy algorithm--step, pressure position and its application

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.