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 无限大数的指数乘法 题解的更多相关文章

  1. POJ 1001 Exponentiation(大数运算)

    POJ 1001 Exponentiation 时限:500 ms   内存限制:10000 K 提交材料共计: 179923   接受: 43369 描述:求得数R( 0.0 < R < ...

  2. [POJ 1001] Exponentiation C++解题报告 JAVA解题报告

        Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 126980   Accepted: 30 ...

  3. [POJ] #1001# Exponentiation : 大数乘法

    一. 题目 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 156373   Accepted: ...

  4. POJ 1001 解题报告 高精度大整数乘法模版

    题目是POJ1001 Exponentiation  虽然是小数的幂 最终还是转化为大整数的乘法 这道题要考虑的边界情况比较多 做这道题的时候,我分析了 网上的两个解题报告,发现都有错误,说明OJ对于 ...

  5. POJ 1001 Exponentiation

    题意:求c的n次幂……要求保留所有小数…… 解法:一开始只知道有BigInteger……java大数+模拟.第一次写java大数……各种报错各种exception……ORZ 没有前导0和小数后面的补位 ...

  6. poj 1001 Exponentiation 第一题 高精度 乘方 难度:1(非java)

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 138526   Accepted: 33859 ...

  7. POJ 1001 Exponentiation(JAVA,BigDecimal->String)

    题目 计算实数a的n次方,具体输出格式看案例 import java.util.*; import java.math.*; public class Main { public static voi ...

  8. POJ 1001 Exponentiation 模拟小数幂

    模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...

  9. POJ 1503 Integer Inquiry(大数相加)

    一.Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exp ...

随机推荐

  1. 浅谈购物车中cookie的使用

    购物车对于电商网站来说是一个非常重要的模块.最近自己的项目中也用到了,所以拿出来说说事! 购物车是用户选择商品的一个缓存的地方.其中包含了商品的基本信息,例如:商品的描述,商品的价格,商品的数量等等. ...

  2. memcached在windows安装

    下载地址: http://s3.amazonaws.com/downloads.northscale.com/memcached-win64-1.4.4-14.ziphttp://s3.amazona ...

  3. 简单学C——第七天

    函数 函数是C语言重要的组成部分,你现在,或者以后(如果C没什么变化的话)所写的任何一个C语言程序都是由一个一个的函数组合在一起的,当然,现在或许你只会在主函数 main中写一个小程序,那么在看了本篇 ...

  4. InputStream的封装类

    package ex03.pyrmont.connector.http; import java.io.IOException; import java.io.InputStream; import ...

  5. 安卓天天练练(十)ListView

    ListView不能和ScrollView同时使用,因为它已经包含了滚动支持. 还有个Gallery http://blog.csdn.net/dazlly/article/details/78639 ...

  6. 升级 Java 编程规范的6个约定

    作为 Java 开发人员,我们会遵循一系列的编码风格和开发习惯.习惯使然是一方面,另一方面,我们也从不停下脚步质疑这些习惯.一段时间以后,笔者养成了一些不同于常人的编码风格和开发习惯.当第一次了解到这 ...

  7. Xstream之常用方式与常用注解

    示例代码 Blog teamBlog = new Blog(new Author("Guilherme Silveira")); teamBlog.add(new Entry(&q ...

  8. Android 一个页面上下两个ListView的页面显示

    Android 一个页面上下两个ListView,当上面的ListView过长时,下面的List基本没有了滑动空间,查阅网上资料,解决办法基本是采用ScrollView做页面滑动,notifyData ...

  9. 14.6.8 Configuring the InnoDB Master Thread IO Rate 配置InnoDB 主线程IO 速率:

    14.6.8 Configuring the InnoDB Master Thread IO Rate 配置InnoDB 主线程IO 速率: 主线程 在InnoDB 是一个线程 执行各种任务在后台. ...

  10. 【转】怎么在Foxmail回复/转发时使用签名?

    原文网址:http://kf.qq.com/faq/120322fu63YV130422yABZRZ.html Foxmail回复/转发时使用签名,可通过在模版中设置签名.如下版本操作方法: 一.fo ...