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的更多相关文章
随机推荐
- type命令详解
转自:http://codingstandards.iteye.com/blog/831504 在脚本中type可用于检查命令或函数是否存在,存在返回0,表示成功:不存在返回正值,表示不成功. $ t ...
- Python爬虫框架Scrapy学习笔记原创
字号 scrapy [TOC] 开始 scrapy安装 首先手动安装windows版本的Twisted https://www.lfd.uci.edu/~gohlke/pythonlibs/#twi ...
- 数据库——SQL数据定义
数据定义 SQL的数据定义语句 操 作 对 象 操 作 方 式 创 建 删 除 修 改 表 CREATE TABLE DROP TABLE ALTER TABLE 视 图 CREATE ...
- 渡过OO的死劫,了解规格的意义——OO第三次博客总结
当熬过了一次次黑暗,迎接我们的却是被扣的惨不忍睹的JSF ┭┮﹏┭┮ 一.总结调研 规格的历史 传统科学的特点是发现世界,而软件的特点是构造世界.软件的最底层就是0,1,两个离散的值.程序设计语言的三 ...
- jQuery全屏滚动插件fullPage使用
1. 引入jquery.js和jquery.fullPage.min.js <script src="jquery.min.js"></script> &l ...
- Hibernate主键注解
http://www.cnblogs.com/hongten/archive/2011/07/20/2111773.html 版权声明:本文为博主原创文章,未经博主允许不得转载.
- 在数组中找出两数之和为10的所有组合(JAVA)
/*利用冒泡排序实现*/ import java.util.Scanner;public class Paixun { public static void main(String[] args) { ...
- golang 反射
参考:|--http://blog.51cto.com/speakingbaicai/1707637 |--https://studygolang.com/articles/6324 反射是在gola ...
- 12_Java面向对象_第12天(构造方法、this、super)_讲义
今日内容介绍 1.构造方法 2.this关键字 3.super关键字 4.综合案例 01构造方法引入 A:构造方法的引入 在开发中经常需要在创建对象的同时明确对象的属性值, 比如员工入职公司就要明确他 ...
- OSG学习:计算纹理坐标
在很多时候,直接指定纹理坐标是非常不方便的,如曲面纹理坐标,只有少数的曲面(如圆锥.圆柱等)可以在不产生扭曲的情况下映射到平面上,其他的曲面在映射到表面时都会产生一定程度的扭曲.一般而言,曲面表面的曲 ...