题目描述:

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. Django 测试开发4 Django 模板和分页器

    Django结合前端框架Bootstrap来开发web页面.pip install django-bootstrap3 在setting.py添加‘bootstrap3’. 继承模板. 在base页面 ...

  2. vue页面刷新重定向

    在App.vue中,添加如下代码: created(){ if(this.$router.path !== '/RealTimeMonitoring'){ this.$router.replace(' ...

  3. FastCGI模式编译安装LAMP+Xcache

    PHP的工作模式:php在lamp环境下共有三种工作模式:CGI模式.apache模块.FastCGI模式.CGI模式下运行PHP,性能不是很好.(已淘汰)FastCGI的方式和apache模块的不同 ...

  4. Java解压tar.Z文件(使用Apache Commons-compress)

    这里使用apache commons compress对.tar.Z格式文件进行解压. 对于一个文件test.tar.Z,我们可以将解压过程理解为: 将test.tar.Z解压为test.tar: 将 ...

  5. 【Leetcode_easy】1122. Relative Sort Array

    problem 1122. Relative Sort Array 参考 1. Leetcode_easy_1122. Relative Sort Array; 2. helloacm; 完

  6. iOS笔试题03

    1. When to use NSMutableArray and when to use NSArray? 1> 当数组元素需要动态地添加或者删除时,用NSMutableArray 2> ...

  7. 使用expect实现自动交互,shell命令行自动输入,脚本自动化,变量引用,expect spawn执行带引号命令,expect 变量为空,不生效,不能匹配通配符*,函数,数组

    背景 有需求,在允许命令或者脚本跳出交互行,需要进行内容输入,但需要人手动输入,不是很方便,此时可以通过expect来实现自动互动交互. expect是一个自动交互功能的工具,可以满足代替我们实际工作 ...

  8. python 创建虚拟环境时报错OSError, setuptools下载失败

    错误信息如下: Using base prefix 'c:\\users\\huful\\appdata\\local\\programs\\python\\python36-32'New pytho ...

  9. windwos提权-CVE-2019-1388

    windwos提权-CVE-2019-1388 guest→system(UAC手动提权) 利用高权限建立一个低权限账户orange 查看权限 win-vomjm1p7c71\orange 下载HHU ...

  10. springboot集成elk 二:springboot + elk 采集日志

    到logstash-2.0.0\bin下新建文件 logstash.conf input { tcp { mode => "server" host => " ...