POJ-1001 Exponentiation 高精度算法
题目链接:https://cn.vjudge.net/problem/POJ-1001
以前写过一个高精度乘法,但是没有小数点,实现起来也没什么难得,
现在把代码都般过来,等会把旧电脑弄一弄,暂时就不写题解了
代码
#include <cstdio>
#include <cstring>
struct BigInteger{
int dot, size;
char num[600];
BigInteger(int size=0, int dot=0):size(size),dot(dot) {
for (int i=0; i<600; i++) num[i]=0;
}
BigInteger(const char str[]):size(0),dot(0) {
for (int i=0; i<600; i++) num[i]=0;
int len=strlen(str);
for (int i=len-1; i>=0; i--)
if (str[i]=='0') len--;
else break;
for (int i=len-1; i>=0; i--){
if (str[i]=='.') dot=len-i-1;
else num[size++]=str[i]-'0';
}
}
BigInteger operator * (const BigInteger &a) const{
BigInteger ans(size+a.size, dot+a.dot);
for (int i=0; i<size; i++){
for (int j=0; j<a.size; j++){
int tmp=num[i]*a.num[j], low=(tmp+ans.num[i+j])%10,
high=(tmp+ans.num[i+j])/10+ans.num[i+j+1];
ans.num[i+j]=low;
ans.num[i+j+1]=high;
}
}
while (ans.num[ans.size-1]==0) ans.size--;
return ans;
}
BigInteger operator ^ (const int n) const{
BigInteger ans("1"), tmp;
memcpy(&tmp, this, sizeof(*this));
for (int i=1; ; ){
if (n&i) ans=ans*tmp;
if ((i<<=1)<=n) tmp=tmp*tmp;
else break;
}
return ans;
}
void show(void){
if (dot>size-1){
printf(".");
for (int i=0; i<dot-size; i++) printf("0");
dot=0;
}
for (int i=size-1; i>=0; i--){
printf("%d", num[i]);
if (dot && i==dot) printf(".");
}printf("\n");
}
};
int main(void){
char inpt[600]; int n;
while (scanf("%s%d", inpt, &n)==2){
BigInteger a(inpt);
(a^n).show();
}
return 0;
}
| Time | Memory | Length | Lang | Submitted |
|---|---|---|---|---|
| None | 352kB | 1436 | G++ | 2018-01-20 15:27:27 |
POJ-1001 Exponentiation 高精度算法的更多相关文章
- POJ 1001 Exponentiation(大数运算)
POJ 1001 Exponentiation 时限:500 ms 内存限制:10000 K 提交材料共计: 179923 接受: 43369 描述:求得数R( 0.0 < R < ...
- [POJ 1001] Exponentiation C++解题报告 JAVA解题报告
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 126980 Accepted: 30 ...
- poj 1001 Exponentiation 第一题 高精度 乘方 难度:1(非java)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 138526 Accepted: 33859 ...
- POJ 1001 Exponentiation 无限大数的指数乘法 题解
POJ做的非常好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. ...
- [POJ] #1001# Exponentiation : 大数乘法
一. 题目 Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 156373 Accepted: ...
- POJ 1001 Exponentiation(JAVA,BigDecimal->String)
题目 计算实数a的n次方,具体输出格式看案例 import java.util.*; import java.math.*; public class Main { public static voi ...
- 1001. Exponentiation高精度运算总结
解题思路 这道题属于高精度乘法运算,要求输入一个实数R一个指数N,求实数R的N次方,由于R有5个数位,而N又特别大,因此用C++自带的数据类型放不下. 解题思路是通过数组储存每次乘积结果和底数的每一位 ...
- poj 1001 求高精度幂(Java, BigDecimal, pow, hasNext, stripTrailingZeros, toPlainString)
求高精度幂 Time Limit: 500MS Memory Limit: 10000K Total Submissions: 180325 Accepted: 43460 Descripti ...
- POJ 1001 Exponentiation
题意:求c的n次幂……要求保留所有小数…… 解法:一开始只知道有BigInteger……java大数+模拟.第一次写java大数……各种报错各种exception……ORZ 没有前导0和小数后面的补位 ...
随机推荐
- js小知识colspan和rowspan
colspan和rowspan这两个属性用于合并表格的列或者行. colspan是"column span"(跨列)的缩写,所以colspan属性用在td标签中,用来跨列合并单元 ...
- [转]m3u8直播测试地址
http://www.cnblogs.com/yuandaozhe/p/5755453.html 调试m3u8的时候需要测试地址 找了几个,备用一下 安徽卫视 http://stream2.ahtv. ...
- Glidar测试安装
在上一篇随笔中,我们完成了对Glidar 仿真器的概念层面的认识.接下来,我们将着手对该该仿真器进行安装测试. 1 依赖库的安装 安装环境为Windows 7 64位+Ubuntu14.04 LTS的 ...
- shell-3.bash的基本功能:输入输出重定向
1. 2. 3. 4.
- 通过curl获取网页访问时间
curl -w %{time_namelookup}:%{time_connect}:%{time_starttransfer}:%{time_total}:%{speed_download}&quo ...
- asp.net core 与EFcore 入门
什么是EFcore? Entity Framework (EF) Core 是轻量化.可扩展和跨平台版的常用 Entity Framework 数据访问技术,EF Core 可用作对象关系映射程序 ( ...
- 路飞学城Python-Day11
[44.函数-生成器] 需求:有一个列表 [0,1,2,3,4,5,6,7,8,9],对这个列表循环+1 li = [0,1,2,3,4,5,6,7,8,9] li = map(lambda x:x+ ...
- C# AES 加解密处理
引言 这是一个有关AES加解密的方法类 一.设置AES加解密密钥:下面列出自己分配的三类密钥 private const string UserKey = "roshan-2015-user ...
- 虚拟机virtualbox,直接复制本机虚拟硬盘vdi使用, 会提示错误的解决方法
提示语句为: 打开硬盘文件D:\Virtualbox\debian9 - 副本.vdi 失败. 明细(D) Cannot register the hard disk ‘D:\Virtualbox\d ...
- GIT配置多用户
在公司工作的时候有时候想提交一点代码到github上,然后一台电脑上就需要配置两个账号分别访问github和公司的gitlab 1. 分别生成两个key 为什么要生成两个key的原因我也不清楚,望路过 ...