POJ 1001 Exponentiation 无限大数的指数乘法 题解
POJ做的非常好,本题就是要求一个无限位大的指数乘法结果。
要求基础:无限大数位相乘
额外要求:处理特殊情况的能力 -- 关键是考这个能力了。
所以本题的用例特别重要,再聪明的人也会疏忽某些用例的。
本题对程序健壮性的考查到达了变态级别了。
某人贴出的測试用例数据地址: http://poj.org/showmessage?message_id=76017
有了这些用例,几下调试就过了。
我关键漏了的用例:
000.10 1
000000 1
000.00 1
.00000 0
000010 1
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std; bool standardizeNumNoDot(string &s)
{
while (!s.empty() && '0' == s[0]) s.erase(s.begin());
if (s.empty()) s = "0";//防止n==1的时候,要输出0
bool notDot = true;
for (unsigned i = 0; i < s.size() && notDot; i++)
{
if ('.' == s[i]) notDot = false;
}
if (notDot) return true; while (!s.empty() && '0' == s[s.size()-1]) s.erase(s.end()-1);
if (!s.empty() && '.' == s[s.size()-1]) s.erase(s.end()-1);
if ( s.empty() ) s = "0";
return false;
} int handleDecimalPoint(string &s)
{
if (standardizeNumNoDot(s)) return 0;
int fraction = 0;
int j = 0;
for (unsigned i = 0; i < s.size() ; i++)
{
if (fraction > 0) fraction++;
if (s[i] != '.') s[j++] = s[i];
else fraction++;
}
s.erase(s.end()-1);
return fraction - 1;
} string mulStr(string a, string b)
{
if ("0" == a || "0" == b) return "0";
int ap = handleDecimalPoint(a);
int bp = handleDecimalPoint(b); string ans(a.size()+b.size(), '0');
for (int i = a.size() - 1; i >= 0 ; i--)
{
int carry = 0;
int an = a[i] - '0';
for (int j = b.size() - 1; j >= 0 ; j--)
{
int bn = b[j] - '0';
int sum = an * bn + carry + ans[i+j+1] - '0';
carry = sum / 10;
ans[i+j+1] = sum % 10 + '0';
}
if (carry) ans[i] += carry;
}
if (ap > 0 || bp > 0) ans.insert(ans.end() - ap - bp, '.');
standardizeNumNoDot(ans);
return ans;
} string sPow(string s, int n)
{
if (s.empty() || "0" == s) return "0";//为了程序的健壮性,一定要加上
if (0 == n) return "1";
if (1 == n) return s; string divideStr = sPow(s, n/2);
divideStr = mulStr(divideStr, divideStr);
if (n % 2) divideStr = mulStr(divideStr, s);
return divideStr;
} void Exponentiation()
{
string s;
int n;
while(cin>>s>>n)
{
standardizeNumNoDot(s);//当n==1的时候
cout<<sPow(s, n)<<endl;
}
} int main()
{
Exponentiation();
return 0;
}
本算法用时0MS,哈哈.
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 : 大数乘法
一. 题目 Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 156373 Accepted: ...
- POJ 1001 解题报告 高精度大整数乘法模版
题目是POJ1001 Exponentiation 虽然是小数的幂 最终还是转化为大整数的乘法 这道题要考虑的边界情况比较多 做这道题的时候,我分析了 网上的两个解题报告,发现都有错误,说明OJ对于 ...
- POJ 1001 Exponentiation
题意:求c的n次幂……要求保留所有小数…… 解法:一开始只知道有BigInteger……java大数+模拟.第一次写java大数……各种报错各种exception……ORZ 没有前导0和小数后面的补位 ...
- poj 1001 Exponentiation 第一题 高精度 乘方 难度:1(非java)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 138526 Accepted: 33859 ...
- POJ 1001 Exponentiation(JAVA,BigDecimal->String)
题目 计算实数a的n次方,具体输出格式看案例 import java.util.*; import java.math.*; public class Main { public static voi ...
- POJ 1001 Exponentiation 模拟小数幂
模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...
- POJ 1503 Integer Inquiry(大数相加)
一.Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exp ...
随机推荐
- ecshop安装程序源码阅读-安装脚本(2)
检查环境变量: 程序目录:图片目录,数据目录,临时目录 模板目录下模板文件 数据库连接函数 数据库配置: 读取数据库列表 创建配置文件(数据库,语言,session有效期等) 创建数据表 创建初始化数 ...
- mysql update不能直接使用select的结果
在sql server中,我们可是使用以下update语句对表进行更新:update a set a.xx= (select yy from b) ;但是在mysql中,不能直接使用set selec ...
- 或许有一两点你不知的C语言特性
关键字篇 volatile关键字 鲜为人知的关键字之一volatile,表示变量是'易变的',之所以会有这个关键字,主要是消除编译优化带来的一些问题,看下面的代码 ; int b = a; int c ...
- mysql的查询缓存模式介绍
mysql的查询缓存 查询是数据库技术中最常用的操作.查询操作的过程比较简单,首先从客户端发出查询的SQL语句,数据库服务端在接收到由客户端发来的 SQL语句后, 执行这条SQL语句,然后将查询到的结 ...
- 关于java reflect
反射的基石 Class类 对比提问: Person类代表人,它的实例对象就是张三,李四这样一个个具体的人, Java程序中的各个Java类属于同一类事物,描述这类事物的Java类名就是Class.对比 ...
- C语言学习总结(三) 复杂类型
第五章.复杂数据类型 (数组.字符串.指针.结构体.枚举.共同体) 1.什么是数组? 概念:把具有相同类型的若干变量按有序的形式组织起来,这些按序排列的同类数据元素的集合称为数组: 按数组元素的类型不 ...
- [转]基于Spring + Spring MVC + Mybatis 高性能web构建
http://blog.csdn.net/zoutongyuan/article/details/41379851/ 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.Angula ...
- Multilingual App Toolkit v2.2 release
Multilingual App Toolkit v2.2 release Today we released Multilingual App Toolkit v2.2. This release ...
- 控制反转(IoC)与依赖注入(DI)
1.控制反转(Inversion of Control)与依赖注入(Dependency Injection) 控制反转即IoC (Inversion of Control),它把传统上由程序代码直接 ...
- 自定义元素–为你的HTML代码定义新元素
注意:这篇文章介绍的 API 尚未完全标准化,并且仍在变动中,在项目中使用这些实验性 API 时请务必谨慎. 引言 现在的 web 严重缺乏表达能力.你只要瞧一眼“现代”的 web 应用,比如 GMa ...