Poj 1001 exponentiation High Precision

Source: Internet
Author: User

// Use high-precision multiplication for this question

# Include <iostream>

# Include <cstring>

Using namespace STD;

# Define x 140

Void multiply (INT num [], int A [], int m) // high-precision Multiplication

{

Int s [x];

Memset (S, 0, sizeof (s ));

Int I, J;

For (j = 0; j <m; j ++)

For (I = 0; I <126; I ++)

S [I + J] + = num [I] * A [J]; // CoreAlgorithmSave the multiplication result to the array s [].

 

Int carry = 0; // table carry

For (I = 0; I <X; I ++)

{

Num [I] = (carry + s [I]) % 10;

Carry = (carry + s [I])/10;

}

}

Int main ()

{

Freopen ("sum. In", "r", stdin );

Freopen ("sum. Out", "W", stdout );

Int N, I;

Char R [7];

While (CIN> r> N)

{

Int num [x];

For (I = 0; I <X; I ++)

Num [I] = 0;

 

Int A [7];

Int dot = 0; // records the decimal places

Int m = 0;

For (I = 5; I> = 0; I --) // converts a character array to an int array.

{

If (R [I]! = '.')

{

A [m] = R [I]-'0 ';

Num [M ++] = R [I]-'0 ';

}

Else

Dot = I; // record the decimal place

}

 

Int q = 0; // The number of zeros in the integer part of a record such as 20.000

For (I = dot-1; I> = 0; I --) // calculate from the first digit of the decimal point.

{

If (R [I] = '0 ')

Q ++;

Else

Break;

}

For (I = 5; I> = 0; I --)

If (R [I]! = '0 ')

Break;

Dot = I-dot;

Int Len = dot * n; // The number of digits in the fractional part of the entire number after calculation

 

For (I = 1; I <n; I ++) // because a number has been saved to num [], you only need to calculate n-1 times.

Multiply (Num, a, m); // use high-precision Multiplication

 

For (I = X-1; I> = 0; I --) // remove the rear zero

If (Num [I]! = 0)

Break;

Int rear = I; // zero position after record

 

For (I = 0; I <= rear; I ++) // remove the front zero

If (Num [I]! = 0)

Break;

Int front = I; // records the front zero position

 

If (rear-front + 1 <Len) // when the 0.0123 ^ 3 =. 000001860867 type, the zero before the decimal point has been removed, you need to add

{

Cout <"."; // 0 is not required for the 0.12 class

 

For (I = len-Rear + front-1; I> 0; I --) // Add the zero difference after the decimal point

Cout <0;

 

For (I = rear; I> = front; I --)

Cout <num [I];

 

Cout <Endl;

}

 

/* Else if (rear-front + 1 = Len) Thought About It. Can this be avoided,

This is because only when the index is set to a moment, but it will not enter this cycle. after submitting the index again, we will find that it is correct.

{

Cout <".";

For (I = rear; I> = front; I --)

Cout <num [I];

Cout <Endl;

}

*/

Else // The decimal part does not need to be filled with zero

{

For (I = rear; I> = front; I --)

{

Cout <num [I];

If (I-front = Len & Len) // If the decimal point is reached, enter.

{

Cout <".";

}

Else if (! Len & I-front = Len)

{// If the input is a 20.00 class, because the zero part of the integer is the same, you must add

For (Int J = 0; j <Q * n; j ++)

Cout <0;

}

}

Cout <Endl;

}

}

 

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.