文章标题
来源:互联网 发布:finalcut pro mac 编辑:程序博客网 时间:2024/06/06 13:22
今天第一次刷oj 就遇到一个求高精度幂的题目,我的思路是先将数据以字符串的形式输入,再判断其是否为整数,转化成整形数组储存,并记录下小数点的位置,最后输出结果。
以下是题目
Input
T输入包括多组 R 和 n。 R 的值占第 1 到第 6 列,n 的值占第 8 和第 9 列。
Output
对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方。输出需要去掉前导的 0 后不要的 0 。如果输出是整数,不要输出小数点
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 <stdio.h>#include <stdlib.h>#include<string.h>void Transform(char a[],int aTran[],int count[]);void preAccumulate(char a[],int aTran[],int count[],int x,int count2[]);void DoAccumulate(int aTran[],int count[],int answer[]);void printCount(int count[],char a[],int x);int locateDecimalPoint(char a[]);int IsInteger(char a[])//判断是否为整数{ char *p=a; while(*p!='\0') { if(*p=='.') break; p++; } if (*p=='\0') return 1; else return 0;}void Transform(char a[],int aTran[],int count[])//将字符型数组转化成整形数组{ int len,i; len=strlen(a); char *p=a; if(IsInteger(a))//判断是否为整数,如果是整数就不用考虑小数点的位置 { for(i=len;i>0;i--) count[i]=aTran[i]=a[len-i]-'0'; count[0]=aTran[0]=len; } else{ i=len-1;count[0]=aTran[0]=len-1; for(p=a;*p!='\0';p++) if(*p!='.') { count[i]=aTran[i]=*p-'0'; i--; } }}int locateDecimalPoint(char a[])//确定小数点位置{ int len; int i=0; len=strlen(a)-1; while(a[len-i]=='0') i++; while(a[len]!='.') len--; return strlen(a)-len-1-i;//返回值是小数位数并且减去末位为0的位数}void DoAccumulate(int aTran[],int count[],int answer[])//用数组进行乘法运算{ int i,j,flag=0; for(i=1;i<=aTran[0];i++) for(j=1;j<=count[0];j++) { answer[j+i-1]+=count[j]*aTran[i]; if(answer[j+i-1]>9) { answer[j+i]+=answer[j+i-1]/10; answer[j+i-1]%=10; flag=1; } else flag=0; if(i==aTran[0]) count[j]=0;//最后一次执行后将原数组元素赋值为0,便于下一次计算 } if(flag==1) answer[0]=aTran[0]+count[0]; else answer[0]=aTran[0]+count[0]-1;}void printCount(int count[],char a[],int x)//打印数字{ int head=500,rear=1,i,location,lo=1; while(count[head]==0) head--; while(count[rear]==0) rear++;//分为整数 ,小于1的小数,大于一的小数情况进行输出 if(IsInteger(a)) for(i=head;i>=rear;i--) printf("%d",count[i]); else{ location=rear+x*locateDecimalPoint(a)-1; if(location>=head) { printf("."); for(i=1;i<location-head+1;i++) printf("0"); } for(i=head;i>=rear;i--) { if(i==location&&location!=head) printf("."); printf("%d",count[i]); } } printf("\n");}//在这里为了节省空间时间选择用两个数组相互传递相乘结果,因此考虑x奇偶性void preAccumulate(char a[],int aTran[],int count[],int x, int count2[]){ int i; for(i=1;i<x;i++) if(i%2==1) DoAccumulate(aTran,count,count2); else DoAccumulate(aTran,count2,count); if(x%2==1) printCount(count,a,x); else printCount(count2,a,x);}int main(){ char a[20]; int aTran[20]; int count[501],count2[501]; int index; int i; while(scanf("%s",a)!=EOF) { scanf("%d",&index); for(i=0;i<=501;i++) count2[i]=count[i]=0; Transform(a,aTran,count); preAccumulate(a,aTran,count,index,count2); } return 0;}
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 实习点滴 - 跟一个蠢bug学习多线程调试、线程安全和可重入函数
- Activity——生命周期
- 纷菲幻剑录 之 十年一剑
- 有三个整数,获取最大值,通过三元运算符完成。
- 解决qt调试时Unknown debugger type "No Engine
- 文章标题
- 一人我饮酒醉歌词
- 动态规划数字三角形问题
- Tempter of the Bone
- HBase相关论文阅读
- 欢迎使用CSDN-markdown编辑器
- P OJ-----3176DP动态规划入门
- ITOO中的权限管理解决方案
- HDU 2102 A计划(广度优先搜索+数据)