Exponentiation

来源:互联网 发布:专业装修设计软件 编辑:程序博客网 时间:2024/05/17 08:59

题目来自杭电:http://acm.hdu.edu.cn/showproblem.php?pid=1063 Exponentiation
Time Limit: 2000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8101 Accepted Submission(s): 2287

Problem 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 R^n. 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

Source

East Central North America 1988

Recommend

PrincetonBoy | We have carefully selected several similar problems for you: 1316 1753 1715 1250 1065

代码:

#include <iostream>#include <cstring>#include <fstream>using namespace std;int main(){    //ifstream cin("in.txt");    //ofstream cout("out.txt");    char str[256];    int temp[256];    int flag;    int len;    int carry;    int digit;    int n;    int i,j;    while(cin >> str >> n)    {        //获取小数点位置,得到小数长度        len = strlen(str);        int flag_point;        i = 0;        while(str[i])        {            if(str[i] == '.')            {                flag_point = i + 1;                break;            }            i++;        }        if(i == len)            flag_point = len;        flag_point = len - flag_point;        //将小数点提出,按整形大数处理        i = 0;        flag = 0;        while(str[i] == '0' || str[i] == '.')            i++;        j = 0;        while(str[i])        {            if(str[i] != '.')                temp[j++] = str[i] - '0';            i++;        }        flag = j;        char ch;        for(i = 0; i < flag / 2; i++)        {            ch = temp[i];            temp[i] = temp[flag - i - 1];            temp[flag - i - 1] = ch;        }        digit = 0;        int weight;        for(i = 0; i < flag; i++)        {            weight = 1;            for(j = 0; j < i; j++)                weight *= 10;            digit += weight * temp[i];        }        //计算        int ntemp;        if(!digit)            cout << "0" << endl;        else if(!n)        {            cout << "1" << endl;        }        else        {            for(i = 0; i < n - 1; i++)            {                carry = 0;                for(j = 0; j < flag; j++)                {                    ntemp =  temp[j] * digit + carry;                    temp[j] = ntemp % 10;                    carry = ntemp /10;                }                while(carry)                {                    temp[flag++] = carry % 10;                    carry /= 10;                }            }            //输出            //后导0位置            int tail = 0;            for(i = 0; i < flag; i++)            {                if(temp[i])                    break;            }            tail = i;            n *= flag_point;            if(flag < n)            {                cout << ".";                for(i = n; i > flag; i--)                    cout << "0";                for(i = flag - 1; i >= tail; i--)                    cout << temp[i];            }            else            {                for(i = flag - 1; i >= n; i--)                    cout << temp[i];                if(i >= tail)                {                    cout << ".";                    for(i = n - 1; i >= tail; i--)                        cout << temp[i];                }            }            cout << endl;        }    }    return 0;}

总结:
依然是大数问题,这里的解决思路是:处理字符串,提取出所需处理数字,即累乘数,同时将数字存入数组中,存储相乘结果(大数一般要用到数组),还要记录小数位数,这个很重要,后面要用到;然后就是相乘,这个不难;最后是输出,注意小数点的位置
下面是几个样例,全过了估计没什么问题:
100.0 2     10000
100 2     10000
001.10 2 1.21
000.01 0 1
011.01 3 1334.633301
000.00 2 0
000000 2 0
00000. 2 0
0.0100 2 .0001
99.999 0 1

0 0
原创粉丝点击