UVa 10106 Product: High precision

Source: Internet
Author: User

10106-product

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem &problem=1047

The Problem

The problem is to multiply two integers X, Y. (0<=x,y<10250)
The Input

The input would consist of a set of pairs of lines. Each line in pair contains one multiplyer.
The Output

For each input pair of lines the output line should consist one integer the product.
Sample Input

12
12
2
222222222222222222222222

Sample Output

144
444444444444444444444444

Complete code:

/*0.012s*/#include <cstdio> #include <cstring> #include <algorithm> using namespace std;  
    
const int MAXN = 505;  
    
Char NUMSTR[MAXN];  
    
    struct Bign {int len, S[MAXN];  
        Bign () {memset (s, 0, sizeof (s));  
    len = 1;  
    } bign (int num) {*this = num;  
    } bign (const char* num) {*this = num;  
        } bign operator = (const int num) {char S[MAXN];  
        sprintf (S, "%d", num);  
        *this = s;  
    return *this;  
        } bign operator = (const char* num) {len = strlen (num);  
        for (int i = 0; i < len; i++) S[i] = num[len-i-1] & 15;  
    return *this;  ///Output Const char* str () const {if (len) {for (int i = 0; i < Len  
            i++) numstr[i] = ' 0 ' + s[len-i-1]; Numstr[len] = ' I ';  
        else strcpy (Numstr, "0");  
    return numstr;  
    ///to the leading 0 void clean () {while (len > 1 &&!s[len-1]) len--;  
        ///plus bign operator + (const bign& b) const {bign C;  
        C.len = 0;  
            for (int i = 0, g = 0; G | | | i < MAX (Len, B.len); i++) {int x = g;  
            if (i < len) x + = S[i];  
            if (I < B.len) x + = B.s[i];  
            c.s[c.len++] = x% 10;  
        g = X/10;  
    return C;  
        ///minus bign operator-(const bign& B) const {bign C;  
        C.len = 0;  
            for (int i = 0, g = 0; i < len; i++) {int x = s[i]-G;  
            if (I < b.len) x-= B.s[i];  
            if (x >= 0) g = 0;  
                else {g = 1;  
            x + 10;  
          }  c.s[c.len++] = x;  
        } C.clean ();  
    return C;  
        ///Multiply bign operator * (const bign& b) const {bign C;  
        C.len = len + B.len; for (int i = 0; i < len; i++) for (int j = 0; J < B.len; J +) C.s[i + j] = = S[i] * b  
        . S[j];  
            for (int i = 0; i < c.len-1 i++) {c.s[i + 1] + = c.s[i]/10;  
        C.s[i]%= 10;  
        } C.clean ();  
    return C;  
        }///except bign operator/(const bign &b) const {bign ret, cur = 0;  
        Ret.len = Len;  
            for (int i = len-1 i >= 0; i--) {cur = cur * 10;  
            Cur.s[0] = s[i];  
                while (cur >= b) {cur = b;  
            ret.s[i]++;  
        } Ret.clean ();  
    return ret; ///mode, Yu bign operatoR% (const bign &b) Const {bign c = *this/b;  
    return *this-c * b;  
        BOOL operator < (const bign& b) Const {if (len!= b.len) return Len < B.len;  
        for (int i = len-1 i >= 0; i--) if (S[i]!= b.s[i]) return s[i] < b.s[i];  
    return false;  
    BOOL operator > (const bign& B) Const {return B < *this; BOOL operator <= (const bign& B) Const {return! (  
    b < *this); BOOL operator >= (const bign &b) Const {return! (  
    *this < b); BOOL operator = = (CONST bign& b) Const {return! b < *this) &&!  
    (*this < b);  
    BOOL operator!= (const bign &a) const {return *this > A | | *this < A;  
} bign operator = = (Const bign &a) {*this = *this + A;        return *this;  
        } bign Operator-= (const bign &a) {*this = *this-a;  
    return *this;  
        } bign operator *= (const bign &a) {*this = *this * A;  
    return *this;  
        } bign operator/= (const bign &a) {*this = *this/a;  
    return *this;  
        } bign operator%= (const bign &a) {*this = *this% A;  
    return *this;  
    
} A, B;  
    int main (void) {char ch;  
        while (~ (ch = getchar ())) {ungetc (ch, stdin);  
        A = gets (NUMSTR), B = gets (NUMSTR);  
    Puts ((A * b). STR ());  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.