Q - High Precision (Large Number) n Power (POJ 1001)

Problem Solution

1. Convert the input string into an integer and store it in number, multiplying the number each time.

2. Inversely store the input string in the integer array a [] to store the results after multiplying the number each time.

3. The specific operation process is to find the n-th power of number, each operation multiplied by the number of each digit in a [].

4. Use point to calculate the number of decimal digits, output a [] in reverse order, and deal with the number of decimal digits.

Code

```#include <stdio.h>
#include <string.h>
const int N = 200;

int main() {
char num[6]; //Store input strings
long number; //Converts the input string to an integer without decimal points
int n,point,count;
int a[N]; //Store operation results
while (scanf("%s%d", num, &n) == 2) {
number = 0; point = -1; count = 0;

//Input Settings
int note1,note2; //Remove the redundant 0 before and after the string
for (int i = 0; i <= 5; i++) {
if (num[i] != '0') {
note1 = i;
break;
}
}
for (int i = 5; i >= 0; i--) {
if (num[i] != '0') {
note2 = i;
break;
}
}
for (int i = note1; i <= note2; i++) {
if (num[i] != '.') {
number = number*10 + (num[i]-48);
}
}
for (int i = note2; i >= note1; i--) {
if (num[i] != '.') {
a[count++] = num[i]-48;
}
}
for (int i = note1; i <= note2; i++) {
if (num[i] == '.') {
point = (note2-i)*n;
break;
}
}

//operation
long s,d;
for (int k = 1; k < n; k++) {
d = 0;
for (int i = 0; i < count; i++) {
s = a[i]*number+d;
a[i] = s%10;
d = s/10;
}
while (d != 0) {
a[count++] = d % 10;
d /= 10;
}
}

//output setting
bool flag = true;
if (count < point) {
flag = false;
printf(".");
while (point != count) {
point--;
printf("0");
}
}
for (int i = count-1; i >= 0; i--) {
if (flag == true && i == point-1) printf(".");
printf("%d", a[i]);
}
printf("\n");
}
return 0;
}
```

Posted by cmason22 on Wed, 09 Oct 2019 17:09:08 -0700