POJ 1001-Exponentiation

POJ 1001-Exponentiation 解题报告

1.原题

Description

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.

This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input

The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

Output

The output will consist of one line for each line of input giving the exact value of Rn. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don’t print the decimal point if the result is an integer.

Sample Input

95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201

Hint

If you don’t know how to determine wheather encounted the end of input: s is a string and n is an integer

2.题目意思

就是计算一个数N的M次方

3.思路

典型的大数题目,基本的数据类型肯定是hold不住的,还是用string,然后再一位一位调整,然后就是计算的时候用

N的M次方 = N的M/2次方 * N的M/2次方

这样更节省时间(本来是不想贴这个代码的,主要是这题太烦了,写了好久,是在不舍得,还是贴一下吧)

4.代码



#include<iostream>
#include<string>
#include<strstream>

using namespace std;

string f_num;
int s_num = 0;

string numList[30];

string changeP(string s) {
    const char* p = s.c_str();
    int len = s.size();
    string k;
    for (int i = 0;i= 10) {
            result -= 10;
            m = 1;
        }
        rp += (result + '0');
        pp++;
        oo++;
    }
    if(m)
        rp += '1';
    return changeP(rp);
}

string calculate(string num,string num1) {
    const char* cList = num.c_str();
    const char* sList = num1.c_str();
    int len = num.size();
    int slen = num1.size();
    string result="";
    int p = 0;
    for (int i = slen-1;i>=0;i--) {
        string ss ;
        int pp = sList[i] - '0';
        int op = 0;
        for (int j = len-1;j>=0;j--) {
            int qq = cList[j] - '0';
            int re = pp * qq + op;
            if(op) op = 0;
            if(re >= 10) {
                op = re / 10;
                re = re % 10;
            }
            ss += (re + '0');
        }
        if(op) ss += (op+'0');
        ss = changeP(ss);
        for (int k = 0;k>f_num>>s_num) {
        int digit = 0;
        const char* cList = f_num.c_str();
        string num;
        int size = f_num.size();
        bool op = true;
        int up = 0;
        for (int i = size-1;i>=0;i--) {
            if(cList[i] == '0' && op){
                up ++;
                continue;
            }
            op = false;
            if(cList[i] == '.') digit = size-1-i;
            else num += f_num[i];
        }
        digit -= up;
        num = changeP(num);
        int mp = atoi(num.c_str());
        strstream ss;
        ss << mp;
        ss >> num;
        string b = getResult(num,s_num);
        const char* plp = b.c_str();
        string p;
        if(digit * s_num >= b.size() && digit != 0) {
            for (int i = 0;i i) p+=plp[i];
                else if(i == b.size()-digit*s_num) p+='.';
                else p+= plp[i-1];
            }
        }else if(digit == 0){
            p = b;
        }
        cout<<p<<endl;
    }

    return 0;
}