题目描述:

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. js vue --- T Z 去掉 T Z 时间

    export const formatDate = (timestamp) => { return timestamp.replace(/T/g,' ').replace(/Z/g,'') } ...

  2. Centos7 安装svn步骤(宝塔面板)

    1.运行以下命令安装SVN yum install subversion 2.运行以下命令创建目录 mkdir /var/svn   3.依次运行以下命令创建版本库 cd /var/svn svnad ...

  3. 调用微信公众平台扫一扫示例及解决接口报错 { "errMsg": "config:invalid signature" }

    一.解决报错问题 单从报错信息 invalid signature 就可以看出是 签名signature 有问题, 查了很多资料,终于知道了问题点, 后台让我直接调接口是不对的,签名是根据请求接口的页 ...

  4. 如何向Makefile传递参数?

    答: 直接在make命令的后面加上参数,如: make HOST_CFLAGS=-I. 注意事项: HOST_CFLAGS变量将会替换相应Makefile中的HOST_CFLAGS,也就是Makefi ...

  5. <JavaScript>调用apply报错:CreateListFromArrayLike called on non-object;

    Function.apply(obj, args)方法能接收两个参数 obj:这个对象将代替Function类里this对象 args:这个是数组,它将作为参数传给Function(args--> ...

  6. 解决一个 MySQL 服务器进程 CPU 占用 100%解决一个 MySQL 服务器进程 CPU 占用 100%的技术笔记》[转]

    转载地址:http://bbs.chinaunix.net/archiver/tid-1823500.html 解决一个 MySQL 服务器进程 CPU 占用 100%解决一个 MySQL 服务器进程 ...

  7. Java 8 Lambda表达式学习和理解

    Java 8 Lambda表达式和理解 说明:部分资料来源于网络 时间:20190704 Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性.Lambda 允许把函数作为一 ...

  8. VM12_pro+Ubuntu16_64+Qt5.12.2环境搭建

    1.准备软件[已经存网盘] 2.安装Vm 3.安装Ubuntu 4.进入linux,修改Qt安装包权限 5.运行Qt 6.第五步会弹出Qt安装的界面,默认安装就行了 7.修改环境变量 sudo ged ...

  9. Windows 10、Ubuntu 18.04 双系统 双硬盘 安装经验总结

    首先说明,我假设读者懂得分区.安装系统,所以不再深入讨论具体操作. 如果需要手把手教程,建议先参考其它Windows中加装Linux的相关文章. 网上其它文章.教程的常见问题是,各家机器配置不一样,安 ...

  10. spark:neither spark.yarn.jars not spark.yarn.archive is set

    1.Spark启动警告:neither spark.yarn.jars not spark.yarn.archive is set,falling back to uploading librarie ...