Exponentiation
Time Limit: 500MS                      Memory Limit: 10000K
Total Submissions: 155886        Accepted: 37967

Description

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 Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input

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.

Output

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.
Sample Input

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

Hint

If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer

难度不算很大,主要是这种高精度的运算不能直接用语言里面的运算符,要从计算的本质出发,用数组加上乘法进位的思想

另外一个是小数点标在哪儿的问题,我处理的时候直接记录每个数应该是有多少位小数点,然后只做整数的乘法,最后算完了才来标小数点。

下面的应该是AC的。

#include <iostream>
using namespace std; class Problem {
char a[10]; //获取字符串
int R[130]; //5位数的25次方,最多只有125位
int tmp_R[130];
int unvar_R[5];
static short int n; //指数
short int m; //小数位
public:
Problem() {
for(int i=0; i<130; i++) {
R[i]=0;
tmp_R[i]=0;
}
}
void getString() {
cin.getline(a,10);
}
void printString() {
cout << "题目给出的字符串为: ";
cout << a << endl;
cout << "所求问题的指数为: ";
cout << n << endl;
cout << "底数的小数位为: ";
cout << m << endl;
cout << "底数的整数形式为: ";
for (int i=5; i>=1; i--) {
cout << R[130-i] << " ";
}
cout << endl;
cout << "不变的乘数: ";
for (int i=5; i>=1; i--) {
cout << unvar_R[5-i] << " ";
}
cout << endl;
}
void allocateValue(char* s, int upper) {
for(int i=0; i<6; i++)
a[i] = s[i];
short int i=10, j=1;
//获取指数n
while(a[i-2] != ' ') {
n += (a[i-2]-48) * j;
j *= 10;
i--;
}
n = upper;
i=5;
//获取小数位数
j=0;
for(i=5; i>=0; i--) {
if(a[i] != '.')
j++;
else
break;
}
m = j;
j=1;
//获得整数段
for (i=5; i>=0; i--) {
if(a[i] == '.')
continue;
else {
R[130-j]=a[i]-48;
unvar_R[5-j]=a[i]-48;
j++;
}
}
}
//两个整数相乘
void Product() {
int temp;
int pos;
//给tmp_R一个初始数据
for(int i=0; i<5; i++)
tmp_R[129-i] = unvar_R[4-i];
//相当于乘unvar_R n-1次
for(int k=1; k<n; k++) {
for(int i=0; i<130; i++)
R[i] = 0;
for(int i=0; i<5; i++) {
for(int j=0; j<125; j++) {
pos=129-i-j;
R[pos] += unvar_R[4-i]*tmp_R[129-j];
}
}
for(int i=129; i>=1; i--) {
temp = R[i];
R[i] = temp%10;
tmp_R[i] = R[i];
R[i-1] += temp/10;
}
}
} void printResult() {
int head=0, tail=0;
//前面部分0的个数
for(int i=0; i<130; i++) {
if( R[i] == 0 )
head = head+1;
else
break;
}
//后面部分0的个数
for(int i=130; i>=1; i--) {
if( R[i-1] == 0 )
tail = tail+1;
else
break;
}
int dotpos;
dotpos = m*n; //小数位应该有的位数 if(head==130) {
cout << "0";
cout << endl;
return;
} if(tail>=dotpos){ //小数点的位数小于尾部0的个数,则为整数
for (int i=head; i<130-dotpos; i++)
cout << R[i];
}
else if(head>=130-dotpos){ //小数的整数部分为0
cout << ".";
for(int i=130-dotpos; i<130-tail; i++)
cout << R[i];
}
else{ //整数部分不为0,小数部分不为0
for(int i=head; i<130-dotpos; i++)
cout << R[i];
cout << ".";
for(int i=130-dotpos; i<130-tail; i++)
cout << R[i];
}
cout << endl;
return;
}
void init(){
for(int i=0; i<130; i++) {
R[i] = 0;
tmp_R[i] = 0;
}
for(int i=0; i<5; i++)
unvar_R[i] = 0;
m=0;
n=0;
} }; short int Problem::n=0; int main(void) {
Problem a;
char s[6];
int n;
while(cin>>s>>n) {
a.getString();
a.allocateValue(s,n);
//a.printString();
a.Product();
a.printResult();
a.init();
}
return 0;
}

PKU OJ Exponentiation的更多相关文章

  1. PKU OJ 1002 487-3279

    PKU OJ 1002 487-3279 487-3279 Description Businesses like to have memorable telephone numbers. One w ...

  2. pku oj overhang叠加卡片求最少的卡片数

    这个估计是里面第二简单的了,因为第一简单的是求a+b 哈哈,一submit就ac了 题目如下: Description How far can you make a stack of cards ov ...

  3. PKU OJ A Bug's life

    http://bailian.openjudge.cn/tm2018/G/ #include <iostream> #include <vector> #include < ...

  4. 刘汝佳黑书 pku等oj题目

    原文地址:刘汝佳黑书 pku等oj题目[转]作者:小博博Mr 一.动态规划参考资料:刘汝佳<算法艺术与信息学竞赛><算法导论> 推荐题目:http://acm.pku.edu. ...

  5. PKU 百练OJ Arbitrage

    http://bailian.openjudge.cn/practice/2240/ #include <iostream> #include <string> #includ ...

  6. PKU 百炼OJ 简单密码

    http://bailian.openjudge.cn/practice/2767/ #include<iostream> #include <cmath> #include ...

  7. PKU 百炼OJ 奖学金

    http://bailian.openjudge.cn/ss2017/A/ #include<iostream> #include <cmath> #include <m ...

  8. 百炼OJ - 1001 - Exponentiation

    题目链接 哇一遍AC的感觉也太爽了吧:) #include <stdio.h> #include <string.h> int times=0; char *myCalc(ch ...

  9. HDU——PKU题目分类

    HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...

随机推荐

  1. 【laravel54】关于用户权限认证RBAC和无限极分类

    1.权限认证方面: https://packagist.org/packages/spatie/laravel-permission 用户认证 HTTP本身是无状态,通常在系统交互的过程中,使用账号或 ...

  2. poj----2155 Matrix(二维树状数组第二类)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16950   Accepted: 6369 Descripti ...

  3. 二叉搜索树 C语言实现

    1.二叉搜索树基本概念 二叉搜索树又称二叉排序树,它或者是一棵空树,或者是一棵具有如下特性的非空二叉树: (1)若它的左子树非空,则左子树上所有结点的关键字均小于根结点的关键字: (2)若它的右子树非 ...

  4. C# MediaPlayer的详细用法

    AxWindowsMediaPlayer的详细用法 作者:龙昊雪 AxWindowsMediaPlayer的详细用法收藏 function StorePage(){d=document;t=d.sel ...

  5. Windows下面安装和配置Solr 4.9(一)

       1.Solr下载 下载地址 :http://lucene.apache.org/solr/   2.解压,测试 在example文件夹中找到start.jar文件,用命令提示符运行这个文件:ja ...

  6. IntelliJ IDEA 14 拉取SVN maven 多模块项目 部署tomcat 详细图解!

    二话不说 进入主题 我们创建空项目实际上是项目空间 进入主界面 想用svn必须先启用它 选择Subversion 拉取 svn项目 你会发现这里检测不到目录 我们进入 File>Seting 里 ...

  7. NRF24L01使用外部中断读取数据的问题

    NRF24L01读取数据不能使用中断的方式,原因如下: 首先NRF24L01中断触发时,IRQ引脚会一直保持低电平直到状态寄存器中的中断标志被重新清零. stm32的外部中断触发方式只有上升沿或者下降 ...

  8. java日志-纯Java配置使用slf4j配置log4j(转)

    工程目录如下 代码里面用的是slf4j,但是想要用log4j来管理日志,就得添加slf4j本来的jar,然后添加log4j和slf4j箱关联的jar即可. 如果是maven项目的话添加下面的依赖即可 ...

  9. Android利用Fiddler进行网络数据抓包【怎么跟踪微信请求】

    主要介绍Android及IPhone手机上如何利用Fiddler进行网络数据抓包,比如我们想抓某个应用(微博.微信.墨迹天气)的网络通信请求就可以利用这个方法. Mac 下请使用 Charles 代替 ...

  10. Android开发10——Activity的跳转与传值

    Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据. 一.Activity跳转 方法一Intent intent = new Intent(A.this, B ...