| ||||||||||
| 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 | |||||||||
所有discuss里的数据都测过 还是过不了 help 给个不对的数据#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define BASE 10
//思路是先按大数乘法来做,该去零的头部去零,一次一次乘法
//因为最后是小数点数出来直接加上去,因为小数点最后如果是同一个数的话不会产生零的
//因为char[]型 每一次运算的话 char[n]-'0' 不方便 所以一次性转好,但是有个缺点就是0 和\0 没分别了
//所以要 数字的长度信息,有一个struct 最终转回来 ‘0’和\0就又有分别了,用'\0'去定长度
int count;
struct V
{
int len;
char data[130];
};
V* getinfo(char p[]) //用作起始输入 如99.999的取得它的信息,以备最后时刻应用下
{
char buffer[10]; //起始必须把结尾的0000都去掉,还有纯int型
int length,i;
count=0;
length=0;
if(p[0]=='.')
{ int x1=strlen(p);
memmove(&p[1],p,x1+1);
p[0]='0';
}
for(i=0;p[i]!='\0';i++)
{
if(p[i]=='.'){count=length;}
else
{
length++;
if(count==0)
buffer[i]=p[i]-'0';
else if(count!=0)
buffer[i-1]=p[i]-'0';
}
}
int count1=0;
if(count!=0)
for(i=length;p[i]=='0';i--)
count1++;
if(count!=0){length=length-count1;count=length-count; } //count记录小数位数,length记录了数字长度
else //count=0表示 为纯int 型
{
}
buffer[length]='\0';
V* tmp;
tmp=(V*)malloc(sizeof(V));
tmp->len=length;
memmove(tmp->data,buffer,length+1);
return tmp;
}
V* calculate(V* a,V* b)
{
int lena=a->len;
int lenb=b->len;
char *x=a->data;
char *y=b->data;
int carry,temp;
char buffer[130]; //buffer[]记录结果数据,两数相乘 最大位数为lena+lenb,+1位用来存放'\0'
int x2; //算出来的结果
for(x2=0;x2<=lena+lenb+1;x2++)
buffer[x2]='\0';
int i,j;
for(i=lena-1;i>=0;i--)
{
for(carry=0,j=lenb-1;j>=0;j--)
{
temp=carry+x[i]*y[j]+buffer[i+j+1]; //(因为i,j都比实际length少1,那么i+j+2是实际的长度,i+j+1是 -1 实际位的下标)
carry=(char)temp/BASE;
buffer[i+j+1]=(char)temp%BASE;
}
buffer[i+j+1]=(char)carry; //这个千万不能忘记,因为,可能最后一位有向前面一位进位的
}
for(i=0;buffer[i]=='\0';i++); //i记录了buffer[]头部有几个0;
V* result=(V*)malloc(sizeof(V)); //用作calculate出来的中间数据,要把头部0去掉,计算长度什么的
if(lena+lenb-i<=0)
{ result->len=1;
result->data[0]='\0';
result->data[1]='\0';
}
else
{ result->len=lena+lenb-i;
buffer[lena+lenb]='\0';
memmove(result->data,&buffer[i],result->len+1);
}
return result;
}
void display(V* a) //输出并且加上小数点
{
int i;
if(count<a->len)
{ for(i=0;i<a->len;i++)
{
if(i==a->len-count) printf(".");
printf("%c",a->data[i]+'0');
}
printf("\n");
}
else
{
printf(".");
for(i=1;i<=count-a->len;i++)
printf("0");
for(i=0;i<a->len;i++)
{
printf("%c",a->data[i]+'0');
}
printf("\n");
}
}
int main()
{
char a[8];
int e;
int k;
while(scanf("%s%d",a,&e)!=EOF)
{
if(e==0)printf("1\n");
else{
V* x=getinfo(a);
V* y=calculate(x,x);
count=count*e;
for(k=2;k<e;k++)
{
y=calculate(x,y);
}
display(y);
free(y);
}
}
return 0;
}
//第二组数据还待解决 另外还有0.0001 0.0这样的数据
////保证每一次calculate的数据都是最漂亮的数据前后都不为零,且是整数 所以中间过程不用考虑标点或者前后的零的情况 只要头和尾考虑一下
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator