1001:Exponentiation

总时间限制: 

500ms

内存限制: 

65536kB

描述

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 Rnwhere R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

输入

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.

输出

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.

样例输入

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

样例输出

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

有点难度的,修修改改,有点杂乱,应该还能简练些吧,用时两天多点啊!

#include<iostream>
#include<cmath> 
#include<cstring>
char a[205],b[10],c[205][205]={'\0'};//a中运算出结果,放在数组c中 
int o=0;//控制数组中的行。 
void multi(char t[],int t1,int m,int i)
{
    for(int l=0;l<m;l++){//a中的每位数乘t1 
        for(int k=1;k<=t1;k++)//做乘法运算,(用的是加法,因为字符乘会超出字符的范围,还有0和‘、0’) 
    {
    c[o][i]+=(t[l]-'0');//换成数 
    if(c[o][i]>=10)//进/位吗 
    {
        c[o][i+1]+=1;
        c[o][i]-=10;
    }
    
    }
    i++;}//next 位 
}//c中存着倒序的运算后的结果。也就是从0开始的是低位,向右是高位 
using namespace std;
int main()
{
    char a1[6];//按标准输入,6个字符 
    int a2;//乘方数 
    while(scanf("%s%d",a1,&a2)!=EOF){//计算每一组数 
    int i=0,j,n,k=0,m=0,t=0,point=0 ;//定义变量 
    m=strlen(a1);//字符不足6时也能计算正确才行 
    for(int x=0;x<m;x++)//小数点在哪里 
    {
    if(a1[m-x-1]=='.')//因为是从0开始,应写成m-1-x还能明白 从后面数的小数点 
    point=x;//小数点的位置数 
    else
    b[i++]=a1[m-x-1];    //上面的i=0,也是为了把字符倒序,使低位在前 
    }//字符放在b数组中 
    a[0]='1';//a中的字符是1,初始化为1 
    a[1]='\0'; 
    for(int x=1;x<=a2;x++){//乘方的运算,开始,从1开始到a2 
    k=0;//从数位0开始 
    m=strlen(a);//初始是1,始用1去乘。先得到字符中的数的倒序  
    n=strlen(b);//b中是去除小数点后的字符串——长度不变化 
    for(i=0;i<n;i++)
    {        //计算大数。 B中的数与先前乘得的数每位相乘 c=a*b
        t=b[i]-'0';//去除字符中的30,是二进制中的数 
           multi(a,t,m,k);
            k++;  //相应的数位也往上长,从0开始的位是低位  
              }
  
    for(int s=0;s<m+n-1;s++)//m+n-1是最大位数 
    {            //数组加30,计算出的数放在a中。 不能用strcpy防止数中有0 
    a[s]=c[o][s]+'0';
    c[o][s]='\0';//c中的那一行再置0 
   }/c中是数,a 中是字符数,能输出的,c中的数要加每位30才能看到是什么 
    if(c[o][m+n-1]>0)//看看最后的一位有吗? 
    {
    a[m+n-1]=c[o][m+n-1]+'0';//放在a中,c中再置0 
    c[o][m+n-1]='\0';
    }
      //  cout<<a<<endl;这时的a也是倒序的 
    }
     int ttt=strlen(a);//a的长度 
if(point>0){      // 有小数点的加入小数点。 
           int ll=a2*point;//指数*小数点 
         for(int h=ttt;h>ll;h--)
           a[h]=a[h-1];//向后移一位 
         a[ll]='.';//加入小数点 
     ttt++;//长度加1 
     }
       char tmp;
    for(int h=ttt-1;h>=ttt/2;h--)
    {       // 数组倒置。 
    tmp=a[h];
    a[h]=a[ttt-1-h];
    a[ttt-1-h]=tmp;}
if(point>0){    //有小数点的后面的数是0的去掉。 
       ttt--;
        while(a[ttt]=='0')//是/不是0,是0循环去0 
    {
        a[ttt]='\0';
        ttt--;
    }
    if(a[ttt]=='.')//小数点后全是0 
    a[ttt]='\0';
    int pp=0;
    if(a[0]=='0'&&a[1]=='.')//无整数部分 
    while(a[pp]!='\0')//小数点向前移一位 
    {
    a[pp]=a[pp+1];//向前移位 
    pp++;}
        }
strcpy(c[o],a);//放在数组中(计算出的数)。 
   memset(a,0,sizeof(a));//a中的数是0 初始,为下次计算准备 
   memset(b,0,sizeof(b));
   memset(a1,0,sizeof(a1)); 
  o++;//数组行加一 
     
}
   for(int i=0;i<o;i++)//输出计算的结果 
   printf("%s\n",c[i]);
     
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值