Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350738
  • 博文数量: 122
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 1191
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 11:12
文章分类

全部博文(122)

文章存档

2010年(122)

我的朋友

分类: C/C++

2010-04-10 20:32:46

一、问题描述

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

 

二、分析解答

这道题是高精度计算的,不算很难,但是很繁琐,尤其是对输入输出的要求。被这道题搞了好久,耐心来,一点一点调试,总会成功的。

三、代码

 

#include<iostream>
#include<string>
#include<math.h>
using namespace std;
char ans[10];
char res[2][205];
__int64 ps;//有几位小数点
int len;//长度,R的有效长度
//计算c = b * a
void Multiply(char * b,int bt,char * a,int at,char * c)
{
    int i,j;
    int up=0;
    for(i=0;i<at;++i)
    {
        up=0;
        for(j=0;j<bt;j++)
        {
            int t;    
            if(c[i+j]==0)
                c[i+j]='0';
            t=(a[i]-48)*(b[j]-48)+c[i+j]-48+up;
            if(t>=10)
            {
                up=t/10;
                t=t%10;
                c[i+j]=t+48;            
                if(j==(bt-1) )
                    c[i+j+1]=(up+48);
            }
            else
            {
                c[i+j]=t+48;
                up=0;
            }

        }
    }
}
int main()
{
    string str;
    int n;
    int i,j;
    int s,t;    
    int pos;

    while(cin>>str>>n)
    {        
        i=5;
        pos=str.find('.',0);
        if(pos<0)//没有小数点

        {
            ps=0;
            //zs=zs*n;//后面为0的总数

        }
        else//有小数点

        {
            ps=(5-pos);
            ps=ps*n;//小数位总数

        }

        memset(ans,0,sizeof(ans));
        memset(res[0],0,sizeof(res[0]));
        memset(res[1],0,sizeof(res[1]));
        t=5;
        s=0;
        while(str[s]=='0' || str[s]=='.')
            s++;        
        j=0;
        for(i=t;i>=s;--i)
        {
            if(str[i]=='.')
                continue;
            ans[j]=str[i];
            j++;
        }
        len=j;
        
        strcpy(res[0],ans);
        strcpy(res[1],ans);

        for(i=2;i<=n;++i)
        {
            memset(res[(i+1)%2],0,sizeof(res[0]));
            Multiply(res[i%2],strlen(res[i%2]),ans,len,res[(i+1)%2]);
        }
        int L=strlen(res[(n+1)%2]);
        int d=(n+1)%2;

        if(ps>0)
        {
            j=0;
            while(res[d][j]=='0')
                j++;

            if(ps>=L)
            {
                printf(".");            
                for(i=ps-1;i>=j ;--i)
                {
                    if(i>=L)
                        printf("0");
                    else
                        printf("%c",res[(n+1)%2][i]);
                }
            }
            else
            {
                if(j>=ps)
                {
                    for(i=L-1;i>=ps;--i)
                        printf("%c",res[(n+1)%2][i]);
                }
                else
                {
                    for(i=L-1;i>=j ;--i)
                    {
                        if(i==ps)
                        {
                            printf("%c.",res[(n+1)%2][i]);
                        }
                        else
                            printf("%c",res[(n+1)%2][i]);
                    }
                }                
            }
        }
        else
        {
            for(i=L-1;i>=0;--i)
            printf("%c",res[(n+1)%2][i]);
        }

        printf("\n");
    }
    return 0;
}


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