问题描述

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的更多相关文章

随机推荐

  1. mysql更新表数据时报错 You can't specify target table 'RES_CATALOG_CLASSIFY' for update in FROM clause

    You can't specify target table for update in FROM clause含义:不能在同一表中查询的数据作为同一表的更新数据. 将sql语句 UPDATE RES ...

  2. bing词典vs有道词典对比测试报告——功能篇之细节与用户体验

    之所以将细节与用户体验放在一起讨论,是因为两者是那么的密不可分.所谓“细节决定成败”,在细节上让用户感受方便.舒适.不费心而且温馨,多一些人文理念,多一些情怀,做出来的产品自然比其他呆板的产品更受欢迎 ...

  3. No.101_第二次团队会议

    时间的敲定 在这一次的会议中,明确了任务目标,将任务进行合理分配,并且规划了整个任务的时间节点,这对团队来说非常重要. 一.最终项目 在上一节课的时候,我们最终没有拿到学霸开发项目,最后爬虫也被选走了 ...

  4. java的第二个实验——JAVA面向对象程序设计

    java的第二个实验——JAVA面向对象程序设计 北京电子科技学院 实     验    报     告 课程:Java程序设计 班级:1352 姓名:林涵锦 学号:20135213 成绩:      ...

  5. 20172321『Java程序设计』课程 结对编程练习_四则运算第二周阶段总结

    20172321『Java程序设计』课程 结对编程练习_四则运算第二周阶段总结 结对伙伴 学号 :20172324 姓名 :曾程 伙伴第一周博客地址: 对结对伙伴的评价:一个很优秀的同学,在这次项目中 ...

  6. linux 常用命令-文件、文件夹管理

    1. 创建文件夹: mkdir dirName 删除文件夹: rm -rf * 删除当前目录下的所有文件以及文件夹(非交互式) rm -r  --recursive 递归式删除所删除目录以及子目录(有 ...

  7. 2018软工实践—Beta冲刺(3)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Beta 冲鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 软件接口编写修正 自动化测试脚本编写 技术文稿更新 展示GitHub当 ...

  8. HDU 4281 Judges' response 状压dp+多旅行商问题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4281 Judges' response Time Limit: 2000/1000 MS (Java ...

  9. Java自学基础用法

    在慕课上面简单学习了一下java语言的用法 简单的用法总结记录一下. 代码(学习输入,输出): package hello; import java.util.Scanner; public clas ...

  10. static作用(修饰函数、局部变量、全局变量)

    转自:http://www.cnblogs.com/stoneJin/archive/2011/09/21/2183313.html 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作 ...