Big Data problems (Compilation)

Source: Internet
Author: User

Today, I told my students about the big data problem. I reviewed the big data problem again and implemented it again using C. The Division is still a bit complicated and I have not figured it out, so we won't miss it anymore. I wrote the addition, multiplication, subtraction, and factorial of large numbers myself, and deepened the problem of large numbers. I still need to accumulate the big precision gradually, java version has been written last time; add your own portal; Java photo is very convenient; Java large numbers

Scope of some basic data types:

INT: 32-bit integer, 4 bytes,-2 ^ 31 ~ 2 ^ 31-1 (-more than 2.1 billion ~ More than 2.1 billion)
Unsigned INT: 4 bytes, 0 ~ 2 ^ 32-1 (0 ~ More than 4.2 billion)-10 digits

64-bit integer of VC:
_ Int64: 8 bytes,-2 ^ 63 ~ 2 ^ 63-1 (-more than 90 billion million ~ More than 90 billion million)
Unsigned _ int64: 8 bytes, 0 ~ 2 ^ 64-1 (0 ~ More than 180 billion million)-20 digits
64-bit integer of G ++:
Long long ==__ int64
Unsigned long = unsigned _ int64

Real Number
Float: 4 bytes, 7-digit valid number
Double: 8 bytes, 15 valid digits
Float is the pursuit of precision. In general, we should choose to use double instead of float;

So when we want to calculate more than 20 bits, we need to use arrays to simulate our computing process;

I chose a relatively basic example of poj practices. I started from the basics. I only needed to understand it. I had to accumulate my own templates and use them flexibly;


2981: large integer addition http://bailian.openjudge.cn/practice/2981
In fact, algorithms such as the addition of large integers are relatively simple. According to our usual vertical calculation method, we use arrays to simulate the addition of large numbers. Note that we should handle the carry problem between them, then pay attention to the input and output, and convert them into the method we are used to for computation. The frontend zero should be processed during the output; It is mainly necessary to understand this processing method; Below is the code: the code is just written and can be optimized. It is not the simplest and easy to understand;
# Include <cstdio> # include <cstring> char a [220], B [220]; int C [220], d [220]; int result [240]; int main () {int I = 0, j = 0, Lena, lenb, N; gets (a); // Save the number with a string and input gets (B ); memset (C, 0, sizeof (c); // initialize memset (D, 0, sizeof (d); memset (result, 0, sizeof (result); Lena = strlen (a); lenb = strlen (B); n = Lena> lenb? LENA: lenb; For (j = 0, I = lena-1; I> = 0; I --) // convert the character array in reverse order to the number array (satisfying our computing habits) c [J ++] = A [I]-'0'; For (j = 0, I = lenb-1; I> = 0; I --) d [J ++] = B [I]-'0'; for (I = 0; I <n; I ++) {result [I] + = C [I] + d [I]; // Add the two arrays if (result [I]> = 10) // process carry {result [I + 1] ++; Result [I]-= 10 ;}} I = N; int flag = 0; while (! Result [I]) // process the front end zero {if (I = 0) {flag = 1; printf ("0") ;} I --;} while (I> = 0 & flag = 0) {printf ("% d", result [I]); I --;} printf ("\ n "); return 0 ;}

2980: Big integer multiplication http://bailian.openjudge.cn/practice/2980
The concept of multiplication and addition is basically the same. Note that the result of multiplication by position I and J is saved in the position of I + J in the result array. The following code is used;
# Include <cstdio> # include <cstring> char a [220], B [220]; int C [220], d [220]; int result [440]; int main () {int I = 0, j = 0, Lena, lenb, N; gets (a); // Save the number with a string and input gets (B ); memset (C, 0, sizeof (c); // initialize memset (D, 0, sizeof (d); memset (result, 0, sizeof (result); Lena = strlen (a); lenb = strlen (B); n = Lena> lenb? LENA: lenb; For (j = 0, I = lena-1; I> = 0; I --) // convert the character array in reverse order to the number array (satisfying our computing habits) c [J ++] = A [I]-'0'; For (j = 0, I = lenb-1; I> = 0; I --) d [J ++] = B [I]-'0'; for (I = 0; I <Lena; I ++) for (j = 0; j <lenb; j ++) result [I + J] + = C [I] * d [J]; // The result of I * j should be saved in the position of I + J for (I = 0; I <n * 2; I ++) {If (result [I]> = 10) // process carry {result [I + 1] + = Result [I]/10; result [I] % = 10;} I = N * 2-1; while (result [I] = 0) I --; // process the front-end zero while (I> = 0) {printf ("% d", result [I]); I --;} return 0 ;}

2736: large integer subtraction http://bailian.openjudge.cn/practice/2736
The idea of subtraction and addition is basically the same; replace addition in a program with subtraction, and pay attention to it during carry processing; The following is the code;
# Include <cstdio> # include <cstring> int main () {// freopen ("1.txt"," r ", stdin); char a [220], B [220]; int C [220], d [220]; int result [240]; int N, I, j, Lena, lenb; scanf ("% d", & N ); while (n --) {scanf ("\ n % s", a); getchar (); scanf ("% s", B); memset (C, 0, sizeof (c); memset (D, 0, sizeof (d); memset (result, 0, sizeof (result); Lena = strlen (); lenb = strlen (B); for (I = lena-1, j = 0; I> = 0; I --) c [J ++] = A [I]-'0'; for (I = lenb-1, j = 0; I> = 0; I --) d [J ++] = B [I]-'0'; for (I = 0; I <Lena; I ++) result [I] = C [I]-d [I]; for (I = 0; I <Lena; I ++) if (result [I] <0) // handle case smaller than zero {result [I] + = 10; // forward one by 10; Result [I + 1] --;} for (I = lena-1 ;! Result [I] & I> 0; I --); // process the front-end zero for (j = I; j> = 0; j --) printf ("% d ", result [J]); printf ("\ n");} return 0 ;}

Big Data factorial: http://acm.nyist.net/JudgeOnline/problem.php? PID = 28 The big number factorial here is equivalent to multiplication multiple times. Here I use the 10000 carry, and the basic idea is the same; The following code is used:
# Include <cstdio> # include <cmath> void factorial (int n) {int A [10000]; int I, j, C, M = 0; A [0] = 1; for (I = 1; I <= N; I ++) {c = 0; For (j = 0; j <= m; j ++) // M indicates the number of digits in the current array {A [J] = A [J] * I + C; C = A [J]/10000; // determine whether carry a [J] = A [J] % 10000; // Save the carry number} If (C> 0) {M ++; A [m] = C ;}} printf ("% d", a [m]); int W = m * 4 + log10 (A [m]) + 1; // here is the total number of digits used to calculate the factorial (not allowed here) for (I = s-1; I> = 0; I --) printf ("% 0.4d ", A [I]); // if the number of digits in the current position is not full 4, make up printf ("\ n"); printf ("% d", W ); // (or not Want this output)} int main () {int m; while (scanf ("% d", & M )! = EOF) {factorial (m);} return 0 ;}

You have to study the division of large numbers yourself ~ Update after study ~









Big Data problems (Compilation)

Related Article

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.