poj1001 高精度

来源:互联网 发布:网络电视怎么连接网络 编辑:程序博客网 时间:2024/05/03 18:59

 

如题:http://poj.org/problem?id=1001

 

  

Exponentiation
Time Limit: 500MS Memory Limit: 10000KTotal Submissions: 142484 Accepted: 34813

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 120.4321 205.1234 156.7592  998.999 101.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.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){...}cwhile(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

East Central North America 1988

 

 

思路:很繁琐的一道题,题目思路很清晰,高精度乘法,找准小数点的位数,输出时加进去小数点。实现起来对小数点位数的处理和各种边界(小数点影响的前导后导0的处理),一个细节处理不好,就崩了。

在每一次的乘法中没有处理前导0,而是在所有运算后同时处理上下界和小数点的位置。确定输出的上界i,下界j。然后输出即可。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

char str[10];
int a[10];
int res[500];

int Multiple(int ma,int mr)
{
    int i,j;
    int k=ma+mr;
    int c[500]={0};
    for(i=1;i<=ma;i++)
        for(j=1;j<=mr;j++)
    {
        c[i+j-1]+=a[i]*res[j];
        c[i+j]+=c[i+j-1]/10;
        c[i+j-1]%=10;
    }
        for(i=1;i<=k;i++)
            res[i]=c[i];
        mr=k;
        return mr;
}
int main()
{
 // freopen("C:\\1.txt","r",stdin);
    while(~scanf("%s",str+1))
    {
        int n;
        cin>>n;
        memset(a,0,sizeof(a));
        memset(res,0,sizeof(res));
        int dot_pos=-1;
        int i,j;
        int len=strlen(str+1);
        int ma=0,mr=0;
        for(i=len;i>=1;i--)
            if(str[i]=='.')
                dot_pos=i;
            else
                a[++ma]=str[i]-'0';
   mr=ma;
   for(i=1;i<=ma;i++)
    res[i]=a[i];
        for(i=0;i<n-1;i++)
           mr=Multiple(ma,mr);
        if(dot_pos==-1)
        {
            for(i=mr;i>=1;i--)
                printf("%d",res[i]);
            printf("\n");
        }
        else
        {
            dot_pos=len-dot_pos;
            dot_pos*=n;
            int up=mr,down=1;
            for(i=1;i<=mr;i++)
                if(res[i]!=0)
                 {
                     down=i;
                     break;
                 }
            for(i=mr;i>=1;i--)
                if(res[i]!=0)
                {
                    up=i;
                    break;
                }
            i=up;
            j=down;
            if(dot_pos>up)
                i=dot_pos;
            if(dot_pos<down)
                j=dot_pos+1;
            while(i>=j)
            {
                if(i==dot_pos)
                    printf(".");
                    printf("%d",res[i]);
                i--;
            }
          printf("\n");
        }
    }
    return 0;
}

 

 

0 0
原创粉丝点击