| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
为什么WA啊,哥们门帮帮忙/*************************************************************
** 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.
**
** 版本:1.0.0
** 注释:北大ACM1001
**------------------------------------------------------------
*************************************************************/
#include<stdio.h>
#include<string.h>
char r[6];//存放底数
unsigned result[156];//存放结果
unsigned temp[156];//存放中间结果
/*************************************************************
** 函数名:len
** 输 入:无
** 输 出:int
** 功能描述:返回result数组的长度
** 用到的全局变量:result
**************************************************************/
int len()
{
int i;
for(i=0;i<=156;i++)
{
if(result[i]!=0)
break;
}
return 156-i;
}
/*************************************************************
** 函数名:multiplication
** 输 入:无
** 输 出:无
** 功能描述:计算r×result,并把结果保留到result中
** 用到的全局变量:r,result,temp
**************************************************************/
void multiplication()
{
int i,j;//循环控制变量
//初始化temp
memset(temp,0,sizeof(temp));
for(i=5;i>=1;i--)//每一轮都用r的一位去和result的各位相乘,从个位开始
{
for(j=155;j>155-len();j--)
{
temp[j+i-5]+=r[i]*result[j];
}
}
//下面的循环统一处理进位问题
for(i=155;i>=0;i--)
{
if(temp[i]>=10)
{
temp[i-1]+=temp[i]/10;
temp[i]%=10;
}
}
//把temp的值赋予result
for(i=0;i<156;i++)
{
result[i]=temp[i];
}
}
void main()
{
int n;//存放幂数
int m;//存放底数小数点后位数
int j;//记录小数点出现位置
int i;//循环控制变量
int endPoint=156;//如果输出是小数点话,记录输出的最终位置
while(scanf("%s%d",r,&n)==2)
{
int bStartOutput=0;
int bInteger=1;
for(j=2;j>0;j--)//小数点只可能出现在【1】、【2】位置
{
if(r[j]=='.')
break;
}
m=5-j;
m=m*n;//小数的数目
//将底数转换成整数,不足六位前面添0
for(i=(j-1);i>=0;i--)
{
r[i+1]=r[i];
}
r[0]='0';
//将字符串数组r中所有数字ascii码转换成实际数字
for(i=0;i<6;i++)
{
r[i]-='0';
}
//初始化result
memset(result,0,sizeof(result));//将result所有成员置为0
n--;//第一次幂结果就是它本身,把它放入result中
for(i=1;i<6;i++)
{
result[i+150]=r[i];
}
while(n--)
{
multiplication();
}
//前移
for(i=1;i<156-m;i++)
{
result[i-1]=result[i];
}
//添加小数点位标志
result[156-m-1]=100;
//判断是否为整数
for(i=156-m;i<156;i++)
{
if(result[i])
{
bInteger=0;
break;
}
}
//输出
if(bInteger)
{
for(i=0;i<156;i++)
{
if(bStartOutput)
{
if(result[i]==100)
break;
else
printf("%d",result[i]);
}
else if(result[i])
{
printf("%d",result[i]);
bStartOutput=1;
}
}
}
else
{
for(i=155;i>=0;i--)
{
if(result[i]==0)
endPoint=i;
else
break;
}
for(i=0;i<endPoint;i++)
{
if(bStartOutput)
{
if(result[i]!=100)
printf("%d",result[i]);
else
printf(".");
}
else if(result[i])
{
bStartOutput=1;
if(result[i]!=100)
printf("%d",result[i]);
else
printf(".");
}
}
}
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator