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的更多相关文章
随机推荐
- shell之arp命令
arp: 显示所有的表项. arp -d address: 删除一个arp表项. arp -s address hw_addr: 设置一个arp表项. 常用参数: -a 使用bsd形式输出.(没有 ...
- Python 命令行解析工具 Argparse介绍
最近在研究pathon的命令行解析工具,argparse,它是Python标准库中推荐使用的编写命令行程序的工具. 以前老是做UI程序,今天试了下命令行程序,感觉相当好,不用再花大把时间去研究界面问题 ...
- java分布式事务,及解决方案
1.什么是分布式事务 分布式事务就是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.以上是百度百科的解释,简单的说,就是一次大的操作由不同的小操作组成 ...
- React Native (0.57)开发环境搭建(注意:Node不要随便更新到最新版,更新完后莫名其妙的问题一大堆)
搭建开发环境 一.安装依赖 必须安装的依赖有:Node.Watchman 和 React Native 命令行工具以及 Xcode. 1.首先安装 Homebrew 2.安装 Node, Watchm ...
- CSS命名规范(规则)常用的CSS命名规则
CSS命名规范(规则)常用的CSS命名规则 CSS命名规范(规则)常用的CSS命名规则 头:header 内容:content/container 尾:footer ...
- TeamWork#3,Week5,Bing Input Method vs Sogou Input Method
现在电脑上用五笔的用户越来越少了,好的拼音输入法也是难求.必应输入法的前身英库拼音输入法来自微软亚洲研究院的多项基础研究成果.最新的必应输入法不仅保留了英库拼音输入法的各项优势,还结合了必应的搜索体验 ...
- Beta 冲刺1
队名:日不落战队 安琪(队长) 过去两天完成了那些任务 修改个人信息界面. 修改手写涂鸦界面. 接下来的任务 改进手写涂鸦,加入其他功能. 还剩下的任务 社交模块功能. 遇到的困难 无. 有哪些收获和 ...
- 【大数据应用技术】作业八|爬虫综合大作业Molly134
本次作业的要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3075 前言:本次作业是爬取CBO中国票房2010-2019年每年 ...
- AVMoviePlayer 视频播放器
AVMoviePlayer 是使用系统框架 MPMoviePlayerController 封装的视频播放器 一.功能: 1.根据手机旋转自由切换横竖屏:2.手势轻点显示/隐藏topView/bott ...
- 旧文备份:CANopen协议中SDO服务
SDO是服务数据对象接口(Service Data Obiect)的缩写,顾名思义提供服务数据的访问接口,服务数据就是一些实时性要求不高的数据,一般是指节点配置参数,因此,SDO一般用来配置和获得节点 ...