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. Quartz1.8.5例子(七)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

  2. SignalR介绍与Asp.net,前台即时通信【转】

    SignalR 是一个asp.net异步库,它提供广播消息到多个client端的机制. SignalR能用来持久客户端与服务端的连接,让我们便于开发一些实时的应用,例如聊天室在线预订系统,股票交易等实 ...

  3. "Cannot convert value '0000-00-00' from column 2 to TIMESTAMP"mysql时间转换bug

    今天在项目中遇到这样的一个bug,Cannot convert value '0000-00-00' from column 2 to TIMESTAMP 仔细一查,经过http://blog.csd ...

  4. BZOJ 2226 LCMSum

    Description Given \(n\), calculate the sum \(LCM(1,n) + LCM(2,n) + \cdots + LCM(n,n)\), where \(LCM( ...

  5. andorid studio

    http://www.cnblogs.com/smyhvae/p/4390905.html

  6. 转-[Python 学习]2.5版yield之学习心得

    在 shhgs 发布了关于< Py 2.5 what’s new 之 yield>之后,原来我不是特别关注 yield 的用法,因为对于2.3中加入的yield相对来说功能简单,它是作为一 ...

  7. 用SharedPreferences保存List(Map(String, String))数据

    原因: SharedPreferences没有保存数组的方法,但是有时候为了保存一个数组而进行序列化,或者动用sqlite都是有点杀猪焉用牛刀的感觉,所以就自己动手改进一下吧. 解决方案: 采用的方式 ...

  8. INDEX FAST FULL SCAN和INDEX FULL SCAN

    INDEX FULL SCAN 索引全扫描.单块读 .它扫描的结果是有序的,因为索引是有序的.它通常发生在 下面几种情况(注意:即使SQL满足以下情况 不一定会走索引全扫描) 1. SQL语句有ord ...

  9. OOD/DDP 中的 SRP 原则

    单一职责原则 SRP(The Single Responsibility Principle):一个类应该只有一个发生变化的原因.这里的变化指职责的变化. SRP 很好理解,它的要求是 让一个类只做一 ...

  10. VisualBox ubuntu14.04 64位 android4.4.4源码编译总结

    转载请保留出处:http://www.cnblogs.com/wi100sh/p/4337907.html 折腾了好几天,今天终于编译通过,用了4个多小时,太不容易了.如下图所示: 软件环境 虚拟机: ...