题目描述:

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. 【Oracle/Java】批量删除16张十万数据的表 单线程耗时45秒 多线程耗时38秒

    昨天做了插入的单线程多线程比较,今天做个删除的. 单线程批量删除类代码: package com.hy.delete.singlethread; import java.sql.Connection; ...

  2. ijkplayer相关

    直播技术总结(二)ijkplayer的编译到Android平台并测试解码库 https://blog.csdn.net/hejjunlin/article/details/55670380

  3. 各种Android UI开源框架 开源库

    各种Android UI开源框架 开源库 转 https://blog.csdn.net/zhangdi_gdk2016/article/details/84643668 自己总结的Android开源 ...

  4. angular之Rxjs异步数据流编程入门

    Rxjs介绍 参考手册:https://www.npmjs.com/package/rxjs 中文手册:https://cn.rx.js.org/ RxJS 是 ReactiveX 编程理念的 Jav ...

  5. colorMatrix色彩旋转设置

    package com.loaderman.customviewdemo; import android.app.Activity; import android.graphics.Bitmap; i ...

  6. osg机械臂施工模拟

    线程 0x2278 已退出,返回值为 0 (0x0). =====IfcTreeWidget==slotObjectsSelected1IfcObjectAttributeExtraction === ...

  7. MERN——MongoDB && React && Node && Express

    原文链接:Let’s build a full stack MongoDB, React, Node and Express (MERN) app github源码地址:jelorivera08/re ...

  8. Java 8 Steam 例子整理

    Java 8 Steam 例子整理 kexue 关注 2016.06.06 17:44* 字数 1860 阅读 3901评论 0喜欢 6 IBM: Java 8 中的 Streams API 详解 为 ...

  9. C# using语句 中间出现exception后 using仍然会dispose

    C# using语句,中间出现exception后,using仍然会dispose,因为: using语句 相当于 try-finally{dispose()}

  10. 偶尔在网上看到的,相对比较好的c#端订单号生成规则

    偶尔在网上看到的,相对比较好的c#端订单号生成规则 public class BillNumberBuilder{     private static object locker = new obj ...