OpenJudge1001Exponentiation
问题描述
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 Rnwhere R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
要求
- 输入
- 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.
- 输出
- 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.
思路
本题为高精度计算问题,需要将输入的字符串转换为数组,按照四则运算的方式进行运算,具体代码如下所示:
#include<iostream>
#include<string>
#include<string.h> using namespace std; struct bign{
int data[];
int length;
int point;
bign(){
memset(data, , sizeof(data));
length = ;
point = ;
}
}; string str;
int n; bign Change(){
bign a;
a.point = str.length() - str.find('.') - ;
int j = ;
for(unsigned int i=;i<str.length();i++){
if(str[str.length() - i - ]!='.'){
a.data[j] = str[str.length() - i - ] - '';
j++;
}
}
a.length = j;
return a;
} bign multi(bign a, bign b){
bign c;
int carry = ;
for(int i=;i<b.length;i++){
c.length = i;
for(int j=;j<a.length;j++){
int temp = a.data[j] * b.data[i] + carry + c.data[i+j];
c.data[c.length++] = temp % ;
carry = temp / ;
}
while(carry != ){
c.data[c.length++] = carry % ;
carry /= ;
}
}
c.point = a.point + b.point;
return c;
} bign power(bign a, int n){
if(n==){
bign d;
d.data[] = ;
d.length = ;
return d;
}
if(n%==){
return multi(a, power(a, n-));
}
else{
return multi(power(a, n/), power(a, n/));
}
} void show(bign rs){
string ans;
int zero = ; //记录尾部0的数目
for(int i=;i<rs.point;i++){
if(rs.data[i]!=){
break;
}
else{
zero++;
}
}
if(str[]!=''){
for(int i=rs.length-;i>=zero;i--){
if(zero==rs.point){
ans.insert(ans.end(), rs.data[i] + '');
}
else{
if(i==rs.point){
ans.insert(ans.end(), rs.data[i] + '');
ans.insert(ans.end(), '.');
}
else{
ans.insert(ans.end(), rs.data[i] + '');
}
}
}
}
else{
ans.insert(ans.end(), '.');
for(int i=rs.point-;i>=zero;i--){
ans.insert(ans.end(), rs.data[i] + '');
}
}
cout<<ans<<endl;
} int main(){
while(cin>>str>>n){
bign bg = Change();
bign rs = power(bg, n);
show(rs);
}
return ;
}在解题过程中,遇到的是尾部零处理和首部零处理问题,要注意的是,除了题中所给的样例,10.00之类的幂次需要考虑,这类测试数据,在尾部零处理中,不应该含小数点。
OpenJudge1001Exponentiation的更多相关文章
随机推荐
- [linux] LVM原磁盘扩容(非增加磁盘)
阿里云机器,ECS运行时磁盘由300G扩容到500,重启机器生效后登录.可以看到/data目录总大小300G,/dev/vdb已经扩容到500G. [root@HD1g-elasticsearch2 ...
- 将本地开发完的SDK代码上传到SVN上面:an error occurred while contacting the repository The server may be unreachable or the URL may be incorrect
将本地开发完的SDK代码上传到SVN上面:an error occurred while contacting the repository The server may be unreachabl ...
- VS code MacOS 环境搭建
环境:MacBook Pro 参考博客 为了动手开发AI代码,我需要安装一个VS code. 开始我以为是安装visual studio呢.我装过visual studio2017. VS code是 ...
- 第二阶段Sprint4
昨天:事实现保存到指定路径,并能够选择播放 今天:放弃改文件名,自动生成,实现暂停后继续录制 遇到的问题:不知道为什么实现不了,不然之前录制的视频就会丢失重新录
- Chapter 2 软件过程
软件发展前期,人们只重视结果而忽略了过程,随着技术的成熟,软件过程的重要性被日益发觉.软件过程是软件工程人员为了获得软件产品而在软件工具的支持下实施的一系列软件工程活动. 软件过程的基本活动包括问题提 ...
- 软工 · 第十二次作业 - Beta答辩总结
福大软工 · 第十二次作业 - Beta答辩总结 写第十二次的时候操作失误直接在Beta版本的博客里改了...第七次冲刺的作业链接补在这里 Beta(7/7) 组长本次博客作业链接 项目宣传视频链接 ...
- salt-api使用
salt-api 基本使用 目前salt API 支持的web模块如下: CherryPy Tornado WSGI 1.安装salt-api salt 使用 CherryPy来实现restful的a ...
- Unity3D游戏开发——访问集中式共享模块的设计模式
什么是设计模式 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类的.代码设计经验的总结.设计模式是开发人员在开发过程中面临的一般问题的解决方案,这些解决方案是众多开发人 ...
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (五) 树莓派单子节点发送数据
本项目中各个节点和树莓派的通信不区分信道,因此如果由树莓派发送给特定节点的数据会被所有节点接收到,因此子节点可以判别该数据是否发给自己的,需要在数据的第二个字节中加入目标节点的编号(第一个字节为源节点 ...
- 5G时代
电信语音承载在CDMA2G网络--所以2G基本没有网络 网络走fdd4g 如果5G时代来临,4g网络可能就会像3G一样的慢