HDU-1063 Exponentiation
2016-02-03 13:58
211 查看
Problem 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 Input95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 |
Sample Output548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 |
思路:
分4种情况处理:
1、整数和小数都为0时直接输出0
2、整数为0,则单独计算答案,在输出相应的答案前输出凑齐小数位数的0.
3、整数非0,小数为0,则直接输出答案
4、整数小数都非0,计算对应n次方的小数位数后,先忽略小数点,将整数小数当成整数来求n次方,根据小数位数在中间输出小数点即可
AC代码:
#include <iostream> #include <string> #include <cstring> #include <cstdio> using namespace std; //计算a^n string get_ans(int a,int n) { int b[150],tmp; for(int i=0;i<150;++i)b[i]=0; b[0]=1; for(int i=0;i<n;++i) { tmp=0; for(int j=0;j<150;++j) { tmp=(b[j]*a)+tmp; b[j]=tmp%10; tmp/=10; } } bool flag = false; string ans; for(int i=149;i>=0;--i) { if(!flag&&b[i])flag=true; if(flag)ans+=('0'+b[i]); } return ans; } int main() { int a,b,c,n,count,tmp; char str[6]; while(cin>>str>>n) { count=strlen(str)-2; b=0;c=0; bool flag=false,Flag=false; for(int i=0;i<strlen(str);++i) { if(str[i]=='.')flag=true; else if(flag) c=10*c+(str[i]-'0'); else { b=10*b+(str[i]-'0'); if(b>0&&!Flag)Flag=true; if(!Flag)count--; } } if(c) { if(b>9)count--; else if(!b)count++; while(c%10==0) { count--; c/=10; } } tmp=1; for(int i=0;i<count;++i)tmp*=10; a=b*tmp+c; count*=n;//总的小数位数 if(!b&&!c){cout<<0<<endl;continue;} if(b&&!c){cout<<get_ans(b,n)<<endl;continue;} if(!b&&c) { cout<<"."; string ans = get_ans(c,n); tmp=count-ans.length(); while((tmp--)>0)cout<<0; cout<<ans<<endl; continue; } string ans = get_ans(a,n); int len=ans.length(); tmp=len-count; for(int i=0;i<len;++i) { if(i==tmp)cout<<"."; printf("%c",ans[i]); } cout<<endl; } return 0; }
相关文章推荐
- swift 下 UNcode字符串 转为中文字符
- 2016年2月小记录
- OI入门
- 如何获得当前屏幕顶端距离整个页面最上端的距离(即js获取浏览器滚动条距离顶端的距离)
- Qt Script文档翻译(一)
- Codeforces 618 D Hamiltonian Spanning Tree
- Linux 启动环境变量PATH设置
- 卸载mysql残留
- c#之string的不可变性2
- java web后台开发规范01
- 转线程安全(1)
- chrome 主进程cpu占用50%怎么办?
- 自定义页面黏性滑动
- MAC快捷键
- Ubuntu下 git 服务器的搭建【转】
- [leetcode]@python 84. Largest Rectangle in Histogram
- Regression 算法
- 即将迎接我们的 3D 音频以及全新的虚拟现实(下)
- Hbase分页实现
- 【一步一步学NPOI】3.设置单元格宽高和边框