High-Precision template pressure code Lite version

Source: Internet
Author: User
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace STD; 5 struct bign {6 int Len; 7 int num [1501]; 8 bool flag; 9 bign () {Len = 1; flag = 0; memset (Num, 0, sizeof num);} 10 bign (int x) {11 if (! X) return; 12 Len = 0; 13 while (x) num [++ Len] = x % 10; X/= 10; 14} 15 }; 16 bool operator <(bign A, bign B) {17 if (. len <B. len) return 1; 18 if (. len> B. len) return 0; 19 for (INT I =. len; I> = 1; I --) if (. num [I]> B. num [I]) return 0; 20 return 1; 21} 22 bool operator = (bign A, bign B) {23 if (. len! = B. Len) return 0; 24 for (INT I = 1; I <= A. Len; I ++) if (A. Num [I]! = B. num [I]) return 0; 25 return 1; 26} 27 bign operator + (bign & A, bign & B) {28 bign ret; 29 int I = 1, X = 0; 30 While (I <=. len | I <= B. len) {31 ret. num [I] =. num [I] + B. num [I] + X; 32 x = ret. num [I]/10; ret. num [I] % = 10; I ++; 33} 34 ret. num [I] = x; ret. len = I; 35 if (! Ret. num [ret. len]) ret. len --; 36 return ret; 37} 38 bign operator * (bign A, bign B) {39 bign ans; 40 int Len =. len + B. len; 41 for (INT I = 1; I <=. len; I ++) {42 int x = 0; 43 for (Int J = 1; j <= B. len; j ++) {44 ans. num [I + J-1] + = (. num [I] * B. num [J] + x); 45 x = ans. num [I + J-1]/10; 46 ans. num [I + J-1] % = 10; 47} 48 ans. num [I + B. len] + = x; 49} 50 while (! Ans. num [Len] & Len> 1) Len --; 51 ans. len = Len; 52 return ans; 53} 54 bign operator-(bign A, bign B) {55 bign ans; 56 if (a = B) Return ans; 57 if (B <A) {58 for (INT I = 1; I <=. len; I ++) {59 If (. num [I] <0). num [I] + = 10,. num [I + 1] --; 60 ans. num [I] =. num [I]-B. num [I]; 61 If (ans. num [I] <0) {62 ans. num [I] + = 10;. num [I + 1] --; // borrow 63} 64} 65} 66 else {67 ans from a's high position. flag = 1; // This number (I mean the ANS) is smaller than zero68 for (INT I = 1; I <= B. len; I ++) {69 If (B. num [I] <0) {70 B. num [I] + = 10; B. num [I + 1] --; 71} 72 ans. num [I] = B. num [I]-. num [I]; 73 If (ans. num [I] <0) {74 ans. num [I] + = 10; B. num [I + 1] --; 75} 76} 77} 78 int Len = max (. len, B. len); 79 while (ans. num [Len] <= 0 & Len> 1) Len --; 80 ans. len = Len; 81 return ans; 82}

Note: This code has omitted bign reading and output functions (very simple, you do not need to use functions), and the main function has been omitted ~

High-Precision template pressure code Lite version

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.