High Accuracy-templates

Source: Internet
Author: User

Alas, I have been busy for a whole day, tired, tired, and tired ....

 

But for the sake of the AC business, it is still worth it ....

 

 

 

 

# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <math. h> # include <assert. h> # include <ctype. h> # include <map> # include <string> # include <set> # include <bitset> # include <utility> # include <algorithm> # include <vector> # include <stack> # include <queue> # include <iostream> # include <fstream> # include <list> using namespace STD; const int maxl = 500; struct bignum {int num [maxl]; int Len ;}; // High Precision comparison> B return 1, A = B Return 0; A <B Return-1; int comp (bignum & A, bignum & B) {int I; If (A. Len! = B. Len) Return (A. Len> B. Len )? 1:-1; for (I = A. len-1; I> = 0; I --) if (A. Num [I]! = B. Num [I]) Return (A. Num [I]> B. Num [I])? 1:-1; return 0;} // Add bignum add (bignum & A, bignum & B) {bignum C; int I, Len; Len = (. len> B. len )? A. len: B. len; memset (C. num, 0, sizeof (C. num); for (I = 0; I <Len; I ++) {C. num [I] + = (. num [I] + B. num [I]); If (C. num [I]> = 10) {C. num [I + 1] ++; C. num [I]-= 10;} If (C. num [Len]) Len ++; C. len = Len; return C;} // high-precision subtraction to ensure that a> = bbignum sub (bignum & A, bignum & B) {bignum C; int I, Len; len = (. len> B. len )? A. len: B. len; memset (C. num, 0, sizeof (C. num); for (I = 0; I <Len; I ++) {C. num [I] + = (. num [I]-B. num [I]); If (C. num [I] <0) {C. num [I] + = 10; C. num [I + 1] --;} while (C. num [Len] = 0 & Len> 1) Len --; C. len = Len; return C;} // High Precision multiplied by low precision. When B is large, the int range may overflow, specific analysis // If B is large, consider B as a high-precision bignum mul1 (bignum & A, Int & B) {bignum C; int I, Len; Len =. len; memset (C. num, 0, sizeof (C. num); // multiply by 0 and return 0 if (B = 0) {C. len = 1; return C;} for (I = 0; I <Len; I ++) {C. num [I] + = (. num [I] * B); If (C. num [I]> = 10) {C. num [I + 1] = C. num [I]/10; C. num [I] % = 10;} while (C. num [Len]> 0) {C. num [Len + 1] = C. num [Len]/10; C. num [Len ++] % = 10;} C. len = Len; return C;} // High Precision multiplied by high precision. Be sure to carry data in time. Otherwise, overflow may occur, but this will increase the complexity of the algorithm, // if it is determined that no overflow will occur, you can change the while value to ifbignum mul2 (bignum & A, bignum & B) {int I, j, Len = 0; bignum C; memset (C. num, 0, Si Zeof (C. num); for (I = 0; I <. len; I ++) {for (j = 0; j <B. len; j ++) {C. num [I + J] + = (. num [I] * B. num [J]); If (C. num [I + J]> = 10) {C. num [I + J + 1] + = C. num [I + J]/10; C. num [I + J] % = 10 ;}} Len =. len + B. len-1; while (C. num [len-1] = 0 & Len> 1) Len --; If (C. num [Len]) Len ++; C. len = Len; return C;} // High Precision divided by low precision. The division result is C, and the remainder is fvoid div1 (bignum & A, Int & B, bignum & C, int & F) {int I, Len =. len; memset (C. num, 0, S Izeof (C. num); F = 0; for (I =. len-1; I> = 0; I --) {f = f * 10 +. num [I]; C. num [I] = f/B; F % = B;} while (LEN> 1 & C. num [len-1] = 0) Len --; C. len = Len;} // high precision * 10 void mul10 (bignum & A) {int I, Len =. len; for (I = Len; I> = 1; I --). num [I] =. num [I-1];. num [I] = 0; Len ++; // If a = 0 while (LEN> 1 &. num [len-1] = 0) Len --;} // High Precision divided by high precision, division result is C, remainder is fvoid div2 (bignum & A, bignum & B, bignum & C, bignum & F) {int I, Len =. len; memset (C. num, 0, sizeof (C. num); memset (F. num, 0, sizeof (F. num); F. len = 1; for (I = len-1; I> = 0; I --) {mul10 (f); // the remainder is multiplied by 10 F each time. num [0] =. num [I]; // then add the remainder to the next digit. // use subtraction to replace the Division while (COMP (F, B)> = 0) {f = sub (F, b); C. num [I] ++;} while (LEN> 1 & C. num [len-1] = 0) Len --; C. len = Len;} void print (bignum & A) // output large number {int I; for (I =. len-1; I> = 0; I --) printf ("% d",. num [I]); Pu TS ("") ;}// convert the string to a large number and the bignum tonum (char * s) {int I, j; bignum A;. len = strlen (s); for (I = 0, j =. len-1; s [I]! = '\ 0'; I ++, j --). num [I] = s [J]-'0'; return a;} void Init (bignum & A, char * s, Int & tag) // convert the string to a large number {int I = 0, j = strlen (s); If (s [0] = '-') {J --; I ++; tag * =-1;}. len = J; For (; s [I]! = '\ 0'; I ++, j --). num [J-1] = s [I]-'0';} int main () {bignum a, B; char S1 [100], S2 [100]; while (scanf ("% S % s", S1, S2 )! = EOF) {int tag = 1; Init (A, S1, tag); // convert a string to a large number of Init (B, S2, tag); A = add (, b); if (. len = 1 &. num [0] = 0) {puts ("0");} else {If (tag <0) putchar ('-'); print ();}} return 0 ;}

 

 

 

 

 

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.