Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157262
  • 博文数量: 35
  • 博客积分: 2011
  • 博客等级: 大尉
  • 技术积分: 345
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-09 21:36
文章存档

2010年(10)

2009年(25)

我的朋友

分类:

2010-12-19 10:33:31

Exponentiation
Time Limit: 500MSMemory Limit: 10000K
Total Submissions: 79498Accepted: 18840

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

Hint

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

while(cin>>s>>n)
{
...
}
c
while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want
/*while(scanf(%s%d",s,&n)!=EOF) //this also work */
{
...
}

Source



//POJ_1001_recursive

#include <math.h>
#include <iostream>
#include <string>

using namespace std;

string AddString(string a, string b)
{
    int len = a.length()>b.length() ? a.length() : b.length();
    if(len == a.length()){
        string auxi(len - b.length(), '0');
        b = auxi + b;
    }
    else{
        string auxi(len- a.length(), '0');
        a = auxi + a;
    }
    int i;
    string result = a;
    int sum = 0;
    int carry = 0;
    for(i=len-1; i>=0; --i){
        sum = a.at(i) + b.at(i) - '0' - '0' + carry;
        carry = sum / 10;
        result[i] = sum % 10 + '0';
    }
    if (carry != 0){
        result = (char)(carry + '0') + result;
    }
    return result;
}

string multiply(const string &a, const string &b)
{
    if(a.length() < 4 && b.length() < 4){//可以直接相乘,这里作为递归的终止条件

        long aInt = atof(a.c_str());
        long bInt = atof(b.c_str());
        char buffer[100];
        sprintf(buffer, "%d", aInt*bInt);
        string result(buffer);
        return result;
    }
    int aLen = a.length();
    int bLen = b.length();
    int aHalfLen = a.length() / 2;
    int bHalfLen = b.length() / 2;
    string str1_1 = a.substr(0, aHalfLen);
    string str1_2 = a.substr(aHalfLen, aLen - aHalfLen);
    string str2_1 = b.substr(0, bHalfLen);
    string str2_2 = b.substr(bHalfLen, bLen-bHalfLen);

    string str11 = multiply(str1_1, str2_1);
    string str12 = multiply(str1_1, str2_2);
    string str21 = multiply(str1_2, str2_1);
    string str22 = multiply(str1_2, str2_2);
    str12.append(aLen-aHalfLen, '0');
    str21.append(bLen-bHalfLen, '0');
    str11.append(aLen+bLen-aHalfLen-bHalfLen, '0');
    string result = AddString(str11, str12);
    result = AddString(result ,str21);
    result = AddString(result ,str22);
    return result;
}

int main()
{
    freopen("E:\\test.txt", "rb", stdin);
    int expo = 0;
    char buff[100];
    while(scanf("%s%d", buff, &expo) != EOF)
    {
        string str(buff);
        int start = 0;
        while(true){//越过开始的0

            if(start <= str.length()-1 && str.at(start) == '0'){
                ++start;
            }
            else{
                break;
            }
        }
        int end = str.length()-1;
        while(true){//越过结束的0

            if(end >= 0 && str.at(end) == '0'){
                --end;
            }
            else{
                break;
            }
        }
        if(start > end){
            cout<<"0"<<endl;
            continue;
        }
        str = str.substr(start, end - start + 1);
        int pos = str.find('.');
        int dot = str.length() - 1 - pos;
        if(pos != -1){//删除小数点

            str = str.erase(pos, 1);
        }

        int time = expo;
        string str2 = str;
        string str1 = str;
        string result = "1";
        if (time%2 != 0){
            result = str;
        }
        time /= 2;
        while(time){
            str2 = multiply(str1, str1);
            if (time%2 != 0){
                result = multiply(result, str2);
            }
            time /= 2;
            str1 = str2;
        }
        str2 = result;
        if( pos == -1 )
        {
        }
        else{
            dot *= expo;
            if(dot > str2.length()){
                string auxi(dot - str2.length(), '0');
                str2 = '.' + auxi + str2;
            }
            else{
                str2.insert(str2.length() - dot, ".");
            }
        }
        if(str2.at( str2.length()-1 ) == '.'){
            str2.erase(str2.length()-1, 1);
        }
        cout<<str2<<endl;
    }
    return 0;
}

注:高精度乘方计算,采用了递归的方法,当数位小于4时即可直接计算,大于4时对其进行递归。

阅读(7456) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~