POJ 1001 Exponentiation 高精度乘法


  题意:输入一个长6位的字符串,表示一个(0.0, 99.999)之间的实数r,然后输入(0, 25]之间的整数n,要求求出r的n次方,输出每一位小数,小数点前面的0不输出,后缀0也不输出。如果结果是整数则直接按整数形式输出(即不输出小数点及其后面的内容)。

  其实就是简单的高精度乘法,不过直接交还是WA了几次,从网上看到的测试数据发现有这样的陷阱:

  1.小数点可以在第一位。例如:".12345"。

  2.输入数据可能有前导0。例如:"000.123"

  3.有可能有不存在小数点的情况(不确定,为了保险还是处理了好)

  4.r=0的情况,题目已经说了这种情况不存在了(既然如此为何要编造这样的测试数据(╯‵□′)╯︵┻━┻)

  我的算法是先把r处理成t*10^(-k)的形式,然后求出t^n,最后根据t^n的长度和k^n的大小确定输出格式(整数/小数点在第一位/小数点在中间)。其中求t^n的方法是先预处理出0~9和t的积,然后每次乘法转化成加法。


#include <iostream>
#include <cmath>
#include <cstdio>
#include <vector>
#include <set>
#include <cstring>
#include <climits>
#include <map>
#include <string>
#include <algorithm>
#include <queue>

using namespace std;

string add(const string& a, const string& b, int pianyi);
string cheng(const string& a);

string chengji[10];

int main() {
  int n, zhishu;
  string s, s1;
  ios::sync_with_stdio(false);
  while (cin >> s >> n) {
    int head = 0, tail = 5, dian = s.find('.');
    while ((s[head] == '0' || s[head] == '.') && head < s.size())
      ++head;
    if (head == s.size()) {
      cout << "0" << endl;
      continue;
    }
    while (s[tail] == '0')
        --tail;
    if (dian == string::npos)
      zhishu = 5 - tail;
    else
      zhishu = tail - dian;
    s1 = s.substr(head, tail - head + 1);
    dian = s1.find('.');
    if (dian != string::npos)
      s1.erase(dian, 1);
    // cout << s1 << " " << zhishu << endl;
    zhishu *= n;
    chengji[0] = "0";
    chengji[1] = s1;
    for (int i = 2; i <= 9; ++i)
      chengji[i] = add(chengji[i - 1], s1, 0);
    string ans = s1;
    for (int i = 2; i <= n; ++i)
      ans = cheng(ans);
    // cout << ans << " " << zhishu << endl;
    if (zhishu == 0) {
      cout << ans << endl;
    } else if (zhishu < ans.size()) {
      for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i];
        if (i + zhishu + 1 == ans.size())
          cout << '.';
      }
      cout << endl;
    } else {
      cout << '.';
      for (int i = 1; i <= zhishu - ans.size(); ++i)
        cout << '0';
      cout << ans << endl;
    }
  }
  return 0;
}

string add(const string & a, const string & b, int pianyi) {
  // b向左偏移了pianyi位。
  int len1 = a.size(), len2 = b.size() + pianyi, len = max(len1, len2) + 1;
  string ans(len, '0');
  int i = len1 - 1, j = len2 - 1, t1, t2, now = len - 1;
  while (i >= 0 || j >= 0) {
    t1 = i < 0 || i >= a.size() ? 0 : a[i] - '0';
    t2 = j < 0 || j >= b.size() ? 0 : b[j] - '0';
    ans[now] = t1 + t2;
    --i;
    --j;
    --now;
  }
  for (int k = len - 1; k > 0; --k) {
    if (ans[k] >= 10) {
      ans[k] -= 10;
      ans[k - 1]++;
    }
    ans[k] += '0';
  }
  while (ans[0] == '0')
    ans.erase(0, 1);
  return ans;
}

string cheng(const string & a) {
  string ans = "0";
  for (int i = a.size() - 1; i >= 0; --i)
    ans = add(ans, chengji[a[i] - '0'], a.size() - 1 - i);
  return ans;
}

   另外:时间限制500ms看起来很可怕,然而其实数据量并不大,放心地使用string类吧。这个程序跑了16ms就过了,可见时间确实不是问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值