博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Poj 1001 / OpenJudge 2951 Exponentiation
阅读量:6339 次
发布时间:2019-06-22

本文共 4058 字,大约阅读时间需要 13 分钟。

1.链接地址:

2.题目:

Exponentiation
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 127573   Accepted: 31149

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 R
n 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 Input

95.123 120.4321 205.1234 156.7592  998.999 101.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201

Hint

If you don't know how to determine wheather encounted the end of input:
s is a string and
n is an integer
C++ while(cin>>s>>n) { ... } c while(scanf("%s%d",s,&n)==2) //to  see if the scanf read in as many items as you want /*while(scanf(%s%d",s,&n)!=EOF) //this also work    */ { ... }

Source

3.思路:

高精度浮点数乘法,利用大整数乘法的基础计算

(1)判断是否为小数,不为小数则转为小数

(2)获取小数点位置,计算出相乘后的小数点的位置。并去除原小数的小数点

(3)利用大整数乘法求结果

(4)补全小数点,并补零

 

tip:注意一下例子

0.0000 1 结果为 0

1.0000 0 结果为 1

0.0000 0 结果为 1

并注意小于1的浮点数要去除前导的0

4.代码:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 7 using namespace std; 8 9 string mul(string str1,string str2) 10 { 11 vector
v_res(str1.size()+str2.size(),0); 12 string::size_type i,j; 13 vector
::size_type k,p; 14 15 reverse(str1.begin(),str1.end()); 16 reverse(str2.begin(),str2.end()); 17 for(i = 0; i != str1.size(); ++i) 18 { 19 for(j = 0; j != str2.size(); ++j) 20 { 21 v_res[i+j] += (str1[i]-'0') * (str2[j] - '0'); 22 } 23 } 24 for(k = 0; k != v_res.size() - 1; ++k) 25 { 26 v_res[k+1] += v_res[k] / 10; 27 v_res[k] = v_res[k] % 10; 28 } 29 30 for(p = v_res.size() - 1; p != -1; --p) 31 { 32 if(v_res[p] != 0) break; 33 } 34 if(p == -1) p = 0; 35 36 string s_res(p+1,'0'); 37 for(k = p; k != -1; --k) s_res[p-k] = char(v_res[k] + '0'); 38 39 40 return s_res; 41 42 } 43 44 string real_mul(string str1,string str2) 45 { 46 string::size_type idx_str1_point = str1.find("."); 47 if(idx_str1_point == string::npos) 48 { 49 str1 += ".0"; 50 idx_str1_point = str1.find("."); 51 } 52 str1.erase(idx_str1_point,1); 53 54 string::size_type idx_str2_point = str2.find("."); 55 if(idx_str2_point == string::npos) 56 { 57 str2 += ".0"; 58 idx_str2_point = str2.find("."); 59 } 60 str2.erase(idx_str2_point,1); 61 62 string::size_type dec_res_len = (str1.size() - idx_str1_point) + (str2.size() - idx_str2_point); 63 64 string res = mul(str1,str2); 65 66 if(res.size() < dec_res_len + 1) 67 { 68 res = string(dec_res_len + 1 - res.size(),'0') + res; 69 } 70 71 res.insert(res.size() - dec_res_len,"."); 72 73 string::size_type idx_res_tail = res.find_last_not_of("0"); 74 res = res.substr(0,idx_res_tail+1); 75 76 if(res[res.size() - 1] == '.') res.erase(res.size()-1); 77 78 return res; 79 } 80 81 int main() 82 { 83 string r; 84 int n; 85 while(cin>>r>>n) 86 { 87 string res = "1"; 88 while(n--) 89 { 90 res = real_mul(res,r); 91 } 92 string::size_type idx_res_pre = res.find_first_not_of("0"); 93 if(idx_res_pre != string::npos) res.erase(0,idx_res_pre); 94 95 96 cout<
<

 

转载于:https://www.cnblogs.com/mobileliker/p/3546535.html

你可能感兴趣的文章
快速把web项目部署到weblogic上
查看>>
高性能的MySQL(5)索引策略-索引和表的维护
查看>>
下一代Windows家庭服务器Vail本地化版本发布
查看>>
SharePoint常见问题二:找不到页面
查看>>
C语言面试
查看>>
基于SSL加密的MySQL主从复制
查看>>
3种方法封锁BT下载
查看>>
C#操作IIS完整解析
查看>>
客户端收发邮件报错:-ERR connection refused
查看>>
U盘病毒原理分析和解决方法
查看>>
谈谈系统架构这个东西
查看>>
thunderbird 导入通讯录乱码问题解决
查看>>
使用LVS+Keepalived实现Mysql的 负载均衡Web集群系统
查看>>
[.Net线程处理系列]专题四:线程同步
查看>>
lync Topology内Replication显示红X
查看>>
yum安装crontab
查看>>
取消列隐藏
查看>>
JVM初探- 内存分配、GC原理与垃圾收集器
查看>>
Ruby和SHELL中如何遍历指定目录的文件
查看>>
LevelDB
查看>>