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的更多相关文章
随机推荐
- ahk打印成pdf记录
软工课程后记: 要求将博客打印成pdf存档.为了偷懒,不想自己点鼠标一个个保存,所以写了一个ahk小程序.博客教程推荐,建议一试,不难.还很方便.我也只学了点点皮毛,满足需求即止. 第一个成功的小例子 ...
- Notes of Scrum Meeting(2014/11/2)
Notes of Scrum Meeting (2014/11/2) 软件工程项目组Sevens开始项目之后的第一次Scrum Meeting报告 会议时间:2014年11月2日 20:00—20: ...
- 第二阶段Sprint冲刺会议8
进展:重新规划主界面,把视频录制暂放到主页面里,先实现功能,视频提醒后期再做.
- Date 类的使用
package com.Date.Math; import java.text.ParseException; import java.text.SimpleDateFormat; import ja ...
- 第一个spring冲刺团队贡献分(80分满分)
团队贡献分(80分满分): 李泳江 24 叶煜稳 26 谢洪跃 18 周伟雄 12
- 关于Keil C51中“ERROR L107: ADDRESS SPACE OVERFLOW ”的总
最近写一个关于单片机播放音乐的程序,出现如下错误: *** ERROR L107: ADDRESS SPACE OVERFLOW ... ... Program Size: data=167.6 xd ...
- Alpha阶段敏捷冲刺①Scrum 冲刺博客
第 1 篇 Scrum 冲刺博客对整个冲刺阶段起到领航作用,应该主要包含三个部分的内容: 各个成员在 Alpha 阶段认领的任务 成员 任务 张晨晨 完成界面设计(前端) 黄登峰 完成界面设计(前端) ...
- Teamcity部署.net服务“无法连接到远程服务器”解决方式
在公司Teamcity上执行自动部署.net服务的时候,发现Teamcity在启动default.aspx的时候报错了,提示:使用“0”个参数调用“GetResponse”时发生异常:“无法连接到远程 ...
- [转帖]超能课堂 CPU制作过程
http://www.expreview.com/50814.html 一般来说,我们对IC芯片的了解仅限于它概念,但是对于已经应用到各式各样的数码产品中IC芯片是怎么来的?大家可能只知道制作IC芯片 ...
- ROC曲线和PR曲线绘制【转】
TPR=TP/P :真正率:判断对的正样本占所有正样本的比例. Precision=TP/(TP+FP) :判断对的正样本占判断出来的所有正样本的比例 FPR=FP/N :负正率:判断错的负样本占所 ...