# Large number of problems

Source: Internet
Author: User
Tags strlen time limit
The addition of a large number

As we all know, the sum of two numbers is summed up from the low, full 10 into one. We can see that the input of two addend is in reverse in the array (deposit 789 and 678 so a[0]=7,a[1]=8,a[2]=9:b[0]=6,b[1]=7,b[2]=8), you can see when we add to add (that is, from a[2]+b[2] Start) will be very troublesome. And if the highest bit to carry will not have a[-1] to save, so we first have to put them in the array, then we can start from a[0]+b[0] The following is the code that will be stored upside down

```void Daoxu (char str[],int x[])
{
int i,j=strlen (str);
char s;
for (i=0;i<j;i++)
{
s=str[i];      Input is string
x[j-i-1]=s-' 0 ';//upside-down to int array
}
}```

Then the next step is to start from the low, full 10 into a

```#include <iostream> #include <cstdio> #include <cstring> using namespace std;
int maxmax=1000;
const int n=2000000;

int y[n];
void Daoxu (char str[],int x[])//The large number of inputs is stored in reverse ({int I,j=strlen (str));
char s;             for (i=0;i<j;i++) {s=str[i];       Input is string x[j-i-1]=s-' 0 ';
int array}}//Deposit end void Qiu (int a[],int b[],int mun[]) {int i;
for (i=0;i<maxmax;i++) {mun[i]=a[i]+b[i];   } for (i=0;i<maxmax+1;i++) {mun[i+1]+=mun[i]/10;//Each person can only leave one number mun[i]=mun[i]%10;
Except for the one that remains, the rest is rounded}} int main () {char S1[maxmax],s2[maxmax];
int mun[maxmax*2];
int X1[maxmax];
int X2[maxmax];  memset (mun,0,sizeof (MUN));//Initialize array memset (x1,0,sizeof (x1));  Initializes an array of memset (X2,0,sizeof (x2));
Initialize an array of cin >> s1 >> S2;             Daoxu (S1,X1);             Reverse-Deposit Array Daoxu (S2,X2);      Reverse-deposit Array Qiu (X1,X2,MUN);     Two large numbers are added int j=maxmax*2-1,i;
while (mun[j]==0)//from the highest bit start to determine the first non-0-bit j--;
for (i=j;i>=0;i--)//Inverted output {printf ("%d", Mun[i]) from a high position;
} return 0; }```
factorial of a large number

First see factorial everyone will do (A For loop can), then when this factorial result is very large ... Over a long long int ...
We know that multiplication is to multiply each multiplier by multiplying the multiplier by 10, and we may as well use this thought to write.

``` #include <iostream> using namespace std; int main () {int n,m;                    Cin >> m;
The input m represents the M-group test data while (m--) {cin >> n;
int a[100001];
int i,j;  int Count=1,next=0,mut;
Mut product next carry count number of digits a[0]=1;  For (i=2, i<=n; i++) {for (j=1; j<=count; J + +) {Mut=a[j-1]*i+next;   The product of each bit equals the current product plus the number of a[j-1] = mut%10;     The current bit can only hold one digit next = MUT/10;  The next sum to be added} while (next)//next>0//The total number of expected digits left is not 0 to indicate that there is not enough overall number {count++;
Total number of digits plus one up to zero a[count-1] = next%10;//"The current bit can only hold one number" next = next/10;//"Next one should be added" }} for (i=count-1; i>=0; i--) cout << a[i];
From high to low output cout << Endl;
} return 0; }```

In order to save time we can also not divide the multiplier into one person, the code is as follows:

```#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main ()
{
int n,i,j;
CIN >> N;
int y[200000];
memset (y,0,sizeof (y));
int count=0,next=0,num; Mut product next carry count number of digits
y[0]=1;
for (i=2;i<=n;i++)       //The same as a single multiply
{for
(j=0;j<=count;j++)
{
num=y[j]*i+next;
y[j]=num%10000;
next=num/10000;
}
while (next)
{
y[++count]=next%10000;
next=next/10000;
}
}                       End
printf ("%d", Y[count]);  Outputs up
to several for (i=count-1;i>=0;i--)//Because except the highest bit y[] are four-bit
printf ("%04d", Y[i]);//less than four bits 0
return 0;
}```
large number multiplication

After reading the large number of additions and the factorial of large numbers, the multiplication of two large numbers is not difficult to understand. is to first the multiplier and the multiplier from the low start to split into a bit, and let every bit of the multiplier multiplied by the multiplier of each, the dislocation add to the following we will show an example of the code problem F: Simple a*b

Time limit: 1 Sec Memory limit: \$ MB Topic description

This is a very simple question that calculates the value of the a*b. input

First line: Number A

Second line: number B

Number B is a positive integer that does not exceed 1000 bits. Output

The value of the a*b, the answer exclusive line. Sample Input

123456
234567 Sample Output

28958703552 Code Show

```#include <iostream> #include <cstdio> #include <cstring> using namespace std;
int maxmax=1000;
const int n=2000000;

int y[n];
void Daoxu (char str[],int x[])//The large number of inputs is stored in reverse ({int I,j=strlen (str));
char s;             for (i=0;i<j;i++) {s=str[i];       Input is string x[j-i-1]=s-' 0 ';
int array}}//Deposit end void Qiu (int a[],int b[],int mun[]) {int i,j;
for (i=0;i<maxmax;i++) {for (j=0;j<maxmax;j++) {mun[i+j]+=a[i]*b[j]; Multiply the multiplier by multiplying each bit by the multiplier per bit//num[i+j] because you want the wrong one add}} for (i=0;i<maxmax*2-1;i++) {mu   n[i+1]+=mun[i]/10;//Each person can only leave a number of mun[i]=mun[i]%10;
Except for the one that remains, the rest is rounded}} int main () {char S1[maxmax],s2[maxmax];
int mun[maxmax*2];
int X1[maxmax];
int X2[maxmax];  memset (mun,0,sizeof (MUN));//Initialize array memset (x1,0,sizeof (x1));  Initializes an array of memset (X2,0,sizeof (x2)); Initialize an array of CIN;> s1 >> S2;             Daoxu (S1,X1);             Reverse-Deposit Array Daoxu (S2,X2);           Reverse-deposit Array Qiu (X1,X2,MUN);
Two large numbers are multiplied by int j=maxmax*2-1,i;
while (mun[j]==0)//from the highest bit start to determine the first non-0-bit j--;
for (i=j;i>=0;i--)//Inverted output {printf ("%d", Mun[i]) from a high position;
} return 0; }```

Related Keywords:

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.