题目描述:

Exponentiation
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 80517   Accepted: 19098
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
我的分析与总结:

基本问题1:数据的接收和存取:

运算因子超出了整型、实型能表示的范围,肯定不能直接用一个数的形式来表示。可选方案:数组和字符串。

1,推荐用字符串接收数据,然后把接收的字符转化为整形赋值到整型数组中。,

2,相乘时,做好数据的逆序处理,因为按照我们算的步骤,数据应该在数组中是逆序存储。

基本问题2:计算结果位数的确定

乘积的位数最大为两个因子的位数之和

乘法代码:(没看太懂)

#include <iostream>
using namespace std;
int main()
{
char r[]={''};//底数
int n=;//指数
while(cin>>r>>n)
{
int m[]={};
int b[]={};
int i,j,m_i;
int m_number=,m_data=;//小数点个数,每次相乘的数据
for(i=;i<;i++)//找到小数点的位置
{
if(r[i]=='.')
{
m_i=i;
break;
}
}
if(i==)//说明没有小数点
{
j=;
while(r[j]=='')
{
r[j]='a';
j++;
}
m_number=;
}
else//说明有小数点
{
i=;
while(i<m_i&&r[i]=='')//小数点之前前导0处理为-1
{
r[i]='a';
i++;
}
i=;
while(i>m_i&&(!r[i]||r[i]==''))//小数点之后的后缀零处理
{
r[i]='a';//赋值为a
i--;
}
for(i=;i>=;--i)
{
if(!r[i]||r[i]=='a')continue;
if(r[i]=='.')break;
m_number++;//判断有多少小数
}
}
for(i=,j=;i<;++i)
{
if(r[i]=='a')continue;
if(r[i]=='.')continue;
if(!r[i])continue;
b[j]=(int)(r[i]-'');//转成int的b
j++;
}
while(j<)
{
b[j]=-;
j++;
}//b可以弄出实数来了
for(i=;i<;++i)
{
if(b[i]==-)continue;
m_data=m_data*+b[i];//相乘的大数
}
for(j=,i=-;j>=&&i>=;--j)
{
if(b[j]==-)continue;
else
{
m[i]=b[j];//赋值给m
i--;
}
}
m_number=m_number*n;
n--;
while(n--&&i!=)//如果出现输入都是0
{
for(j=i+;j<;j++)//有这么多位
{
int tem=m[j]*m_data;//每一位与数相乘
m[j]=;
if(tem==)m[j]=;
else
{
int t=j;
while(tem/!=||tem%!=)
{
m[t]=m[t]+tem%;//将每一位都分离出来存放在不同的单元
int tt1=t;
while(m[tt1]>)//对超过9的处理
{
int tem2=m[tt1];
m[tt1]=tem2%;//得到这位的数据
tt1--;
m[tt1]=m[tt1]+tem2/;//往前加进的位数
}
t--;
tem=tem/;
}
}
}
i=;
while(m[i]==)i++;//找到m中第一个非零的位置
i--;
}
i=;
while(i<&&m[i]==)
i++;
if(m_number==)//对没有小数点的处理
{
if(i==)cout<<<<endl;
else
{
for(;i<;++i)cout<<m[i];
cout<<endl;
}
}
else//有小数点的处理
{
if(m_number>-i)//250-i表示在i之前有i个零,有250-i个非零
{//小数点超过相乘得到的实数,则补零
cout<<".";
while(m_number-->-i)
cout<<;
for(;i<;i++)
cout<<m[i];
cout<<endl;
}
else
{
for(;i<;++i)
{
if(m_number==-i)cout<<".";//包含处理等于
cout<<m[i];
}
cout<<endl;
}
}
}
return ;
}

北京大学1001ACM——高精度类型题总结的更多相关文章

  1. uva 748 Exponentiation 浮点数乘方运算 高精度水题

    输入的前六位数表示一个小数,然后输入一个数表示几次方.要求用高精度算出结果. 高精度水题,主要注意处理小数点,先在输入时把小数点提取出来并记录位置,用普通乘法计算出结果后由后向前计算位置添加小数点. ...

  2. leetcode 字符串类型题

    1,Vaild Palindrome bool isPalindrome(string& s) { transform(s.begin(), s.end(), s.begin(), tolow ...

  3. java 笔记(1)-—— JVM基础,内存数据,内存释放,垃圾回收,即时编译技术JIT,高精度类型

    1.java中5个存放数据的地方: (1).寄存器(Registers):位于CPU内部,是速度最快的存储区,但是数量和容量有限.在java中不能直接操作寄存器. (2).栈(Stack):栈位于通用 ...

  4. hdu 2987最大权闭合图模板类型题

    /* 最大权闭合图模板类型的题,考验对知识概念的理解. 题意:如今要辞退一部分员工.辞退每个员工能够的到一部分利益(能够是负的),而且辞退员工,必须辞退他的下属.求最大利益和辞退的最小人数. 最大权闭 ...

  5. leetcode 链表类型题总结

    链表测试框架示例: // leetcodeList.cpp : 定义控制台应用程序的入口点.vs2013 测试通过 // #include "stdafx.h" #include ...

  6. 高精度水题(POJ2109)

    题目链接:http://poj.org/problem?id=2109 double 可以虽然可以表示10^-307~~~10^208,但是精确度只有16位,这个题有bug. #include < ...

  7. leetcode 动态规划类型题

    1,Triangle int mininumTotal(vector<vector<int>>& triangle) { ; i >= ; --i) { ; j ...

  8. 【高精度&想法题】Count the Even Integers @ICPC2017HongKong/upcexam5563#Java

    时间限制: 1 Sec 内存限制: 128 MB Yang Hui's Triangle is defined as follow. In the first layer, there are two ...

  9. leetcode 树类型题

    树的测试框架: // leetcodeTree.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...

随机推荐

  1. sqlserver 触发器语法

    语法:Trigger on an INSERT, UPDATE, or DELETE statement to a table or view (DML Trigger)CREATE TRIGGER ...

  2. office web apps 在线问答预览

    最近在做项目时,需要用到在线文档预览,看过明道的一篇搭建office web apps服务的文章,但是由于时间的关系,没有仔细研究,这几天有时间,就拿出来研究了下,折腾了几天终于完成了部署,然后就搬过 ...

  3. java说明文档制作

    A:对工具类加入文档注释 B:通过javadoc命令生成说明书 * @author(提取作者内容) * @version(提取版本内容) * javadoc -d 指定的文件目录 -author -v ...

  4. Java Web接入支付宝扫码付款API(使用SDK证书配置properties )

    一.支付宝当面付功能Demo 官方文档https://docs.open.alipay.com/194/106078/ ps:因为沙箱环境一经配置了证书公钥则无法重置 第一步下载项目SKD&D ...

  5. Django和Flask这两个框架对比

    Flask 在 Django 之后发布,现阶段有大量的插件和扩展满足不同需要 Django发布于2005年,Flask创始于2010年年中. Django功能大而全,Flask只包含基本的配置,  D ...

  6. delphi字符串分隔函数用法实例

    这篇文章主要介绍了delphi字符串分隔函数用法,通过自定义函数SeparateTerms2实现将字符串分割后存入字符串列表的功能,具有一定的实用价值,需要的朋友可以参考下 本文实例讲述了delphi ...

  7. Python 解析式、生成器

    标准库datetime datetime模块 对日期.时间.时间戳的处理 datetime类 类方法 today() 返回本地时区当前时间的datetime对象 now(tz=None) 返回当前时间 ...

  8. rdd的元素打印

    在集群上运行spark程序时,rdd的操作都在worker机上,因此输出rdd的元素将在worker机的标准输出上进行,驱动节点上不会运行,故直接才程序中写如下代码 rdd.foreach(print ...

  9. AssetsLibrary 实现访问相册,选取多张照片显示

    #import "ViewController.h" #import <AssetsLibrary/AssetsLibrary.h> /** * ALAssetsLib ...

  10. 无缝轮播 css3

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...