Uva 748 Exponentiation (高精度幂运算)

来源:互联网 发布:php文件上传漏洞 编辑:程序博客网 时间:2024/06/02 04:15
C - Exponentiation

Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu

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 \le 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 Rn. Leading zeros and insignificant trailing zeros should be suppressed in the output.

Sample Input

95.123 120.4321 205.1234 156.7592  998.999 101.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201


问题描述:

这题主要是一道高精度的的大数幂运算问题


题目要求:

输入两个数浮点数R,和整数n,求R^n

其中0.0 < R < 99.999   $0 < n \le 25$

题目解析:

这题题目的时间要求比较长3000MS,所以可以用大数相乘,再循环n次,得出的大数没有小数点。

所以需要先记录下R小数点的位置,小数点之后的长度*n,就是小数点的位置。


#include<string>#include<iostream>#include<stdio.h>#include<string.h>using namespace std;const int MAX = 1000;void getNum(int num[],string str) {int len = str.size();for(int i=0;i<len;i++) {num[len-1-i]=str[i]-'0';}}void multiSolve(int num1[],int num2[],int sum[]) {for(int i=0;i<MAX;i++)for(int j=0;j<MAX;j++) {sum[i+j] += num1[i]*num2[j];}for(int i=0;i<MAX*2-1;i++) {sum[i+1] += sum[i]/10;sum[i] = sum[i]%10;}}string multi(string str1,string str2) {int num1[MAX],num2[MAX],sum[MAX*2];memset(num1,0,sizeof(num1));memset(num2,0,sizeof(num2));memset(sum,0,sizeof(sum));getNum(num1,str1);getNum(num2,str2);multiSolve(num1,num2,sum);int count = MAX*2 - 1;while(sum[count]==0) {count--;}string strSum;char dig;for(int i=count ;i >= 0; i--) {dig=sum[i]+'0';strSum.push_back(dig);}return strSum;}string power(string str,int pow){int after;int doe;int pos = str.find('.');int lenx;int len_str = str.size();if(pos != string::npos) {str.erase(pos,1);lenx=str.size();after = lenx - 1;doe = len_str - pos -1;}else {doe=0;}string sum="1";for(int i=0; i<pow ; i++) {sum=multi(sum,str);}int len = sum.size();string result;if(str[0] >= '1') {for(int i=0 ; i<len ;i++) {result.push_back(sum[i]);if( i== len - pow*doe -1 && doe != 0 ) {result.push_back('.');}}}else {result.push_back('.');for(int i=0;i< pow*after-len;i++) {result.push_back('0');}result += sum;}return result;}int main(){string str;string result;int pow;while(cin>>str>>pow) {getchar();result = power(str,pow);int len = result.size();for(int i=len-1;i >= 0;i--) {if(result[i] == '0')result.erase(i,1);elsebreak;}cout<<result<<endl;}return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 刺客信条起源育碧需要激活码怎么办 电脑连不上网怎么办wifi可以用 电脑登录账号密码错误锁定了怎么办 白色T恤衫上沾上黑色的黄油怎么办 家教遇到成绩好的学生该怎么办 跟越南人离婚孩子中国户口9怎么办 老婆是个越南人至今没户口怎么办 等离子屏z板链接处排线打火怎么办 等离子自动调焊的成形不好怎么办 村里内村道路中间被抢占了怎么办 华为换电池之后卡没反应怎么办 汽车钥匙换电池后没反应怎么办 汽车解锁换电池后没反应怎么办 包裹显示待收件人向海关申报怎么办 在越南签证被公安扣了怎么办 酷派手机收不到验证码怎么办 苹果想把图片上的字盖上怎么办 婴儿自己把眼珠子抠红了怎么办 如果美陆战队员进入台湾那怎么办? 顺产生完小孩吸不通奶怎么办 耐克空军一号白色底发黄怎么办 中行网银u盾丢了怎么办 有人用你的手机号码不停注册怎么办 获得公开你微信头像的权限是怎么办 手机能进的网站电脑进不去怎么办 苹果8p下不了微信怎么办 苹果手机版本过底不能下微信怎么办 手机打开视频跳转到qq是怎么办 淘宝店铺显示服务竟然出错了怎么办 母羊下完羊羔把羊衣吃了怎么办? 移植后56天有黑色东西怎么办 我家的金丝熊浑身都是尿怎么办 一键启动车钥匙丢了怎么办 把爷爷的遗物弄丢了怎么办 如果你娶了一个傻子你怎么办 在国外订机票手机收不到信息怎么办 网上买机票名字写错了怎么办 买机票名字错了一个字怎么办 微店没收到货却显示已收货怎么办? 手机存的照片误删了怎么办 魔兽世界把要用的装备分解了怎么办