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 ...
随机推荐
- "System.Web" 中不存在类型或命名空间
System.Web”中不存在类型或命名空间名称script /找不到System.Web.Extensions.dll引用 添加引用就行了...“添加引用→.Net→System.Web.Ente ...
- Python处理XML
在Python(以及其他编程语言)内有两种常见的方法处理XML:SAX(Simple API for XML)和DOM(Document Object Model,文档对象模型).SAX语法分析器读取 ...
- bzoj 3240: [Noi2013]矩阵游戏 矩阵乘法+十进制快速幂+常数优化
3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 613 Solved: 256[Submit][Status] ...
- ViewController 的loadView、viewDidLoad、viewDidUnload分别是什么时候调用的,在自定义ViewCointroller时在这几个函数中应该做什么工作?
由init.loadView.viewDidLoad.viewDidUnload.dealloc的关系说起 init方法 在init方法中实例化必要的对象(遵从LazyLoad思想) init方法中初 ...
- Psychos in a Line
Codeforces Round #189 (Div. 1) B:http://codeforces.com/problemset/problem/319/B 题意:每一ROUND如果某个人的数字大于 ...
- 将SALT_STACK的JOB-CACHE放到数据库中,而建库用DJANGO的ORM完成
下面包括了SALT_MASTER的配置,及DJANGO的ORM里更改默认表名称,更改默认字段名称(里面有个RETURN),更改默认ID索引... 一个下午有和它磨来磨去... 感谢鹏龙,感谢高远..: ...
- 几个字符串的误区,以及setlocale函数的使用
转自 http://www.blogjava.net/baicker/archive/2007/08/09/135642.html 转自 http://witmax.cn/character-enco ...
- 用SharedPreferences保存List(Map(String, String))数据
原因: SharedPreferences没有保存数组的方法,但是有时候为了保存一个数组而进行序列化,或者动用sqlite都是有点杀猪焉用牛刀的感觉,所以就自己动手改进一下吧. 解决方案: 采用的方式 ...
- 执行计划之CONCATENATION
CREATE TABLE T_CONCAT (ID NUMBER, NAME VARCHAR2(30), TYPE VARCHAR2(30)); INSERT INTO T_CONCAT SELECT ...
- Unity 物体围绕圆周运动
用Unity开发游戏中,经常会有搜寻的功能,这时候我们需要一个放大镜的图标在那圆周运动.写了相关脚本直接挂载在要圆周运动的物体上即可: using UnityEngine; using System. ...