UVa 748 - Exponentiation

题目链接:UVa 748 - Exponentiation

求一个小数的幂,说白了就是先记下来小数点的位置,然后使用使用高精度连乘,估计这里可以使用快速幂,嫌麻烦没试。。最后添加上小数点就可以了。注意一下题目例子中的几种特殊情况该怎么处理。

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

const int maxn = 200;
const int maxm = (1 << 31) - 1;
int cnt,r;

struct bign{
  int len, s[maxn];

  bign()
  {
    memset(s, 0, sizeof(s));
    len = 1;
  }

  bign(int num)
  {
    *this = num;
  }

  bign(const char* num)
  {
    *this = num;
  }

   bign operator = (const char* num)
  {

    len = strlen(num);
    int j;
    for(j = 0;j < len;j++)
    {
        if(num[j] != '0')
            break;
    }
    for(int i = 0; i < len - j; i++)
        s[i] = num[len-i-1] - '0';
    len = len - j;
    return *this;
  }


  string str() const
  {
    string res = "";
    int i,j;
    for(i = 0; i < len; i++)
    {
        res = (char)(s[i] + '0') + res;
        if(i + 1 == r)
            res = '.' + res;
    }
    if(len < r)//变成纯小数
    {
        for(int i = 0; i < r - len;i++)
            res = "0" + res;
        res = "." + res;
    }
    for(j = res.length() - 1;j > 0 ;j--)
    {
        if(res[j] != '0')
            break;
    }
    string _res = res;
    if(j != res.length() - 1)//去除尾缀零
    {
        if(res[j] == '.')
            _res = res.substr(0,j);
        else
            _res = res.substr(0,j + 1);
    }
    if(_res == "") _res = "0";
    return _res;
  }

  bign operator + (const bign& b) const
  {
    bign c;
    c.len = 0;
    for(int i = 0, g = 0; g || i < max(len, b.len); i++)
    {
      int x = g;
      if(i < len) x += s[i];
      if(i < b.len) x += b.s[i];
      c.s[c.len++] = x % 10;
      g = x / 10;
    }
    return c;
  }

  void clean()
  {
    while(len > 1 && !s[len-1]) len--;
  }

  bign operator * (const bign& b)
  {
    bign c; c.len = len + b.len;
    for(int i = 0; i < len; i++)
      for(int j = 0; j < b.len; j++)
        c.s[i+j] += s[i] * b.s[j];
    for(int i = 0; i < c.len-1; i++)
    {
      c.s[i+1] += c.s[i] / 10;
      c.s[i] %= 10;
    }
    c.clean();
    return c;
  }

  bign operator - (const bign& b)
  {
    bign c; c.len = 0;
    for(int i = 0, g = 0; i < len; i++)
    {
      int x = s[i] - g;
      if(i < b.len) x -= b.s[i];
      if(x >= 0) g = 0;
      else
      {
        g = 1;
        x += 10;
      }
      c.s[c.len++] = x;
    }
    c.clean();
    return c;
  }

  bool operator < (const bign& b) const
  {
    if(len != b.len) return len < b.len;
    for(int i = len-1; i >= 0; i--)
      if(s[i] != b.s[i]) return s[i] < b.s[i];
    return false;
  }

  bool operator > (const bign& b) const
  {
    return b < *this;
  }

  bool operator <= (const bign& b)
  {
    return !(b > *this);
  }

  bool operator == (const bign& b)
  {
    return !(b < *this) && !(*this < b);
  }

  bign operator += (const bign& b)
  {
    *this = *this + b;
    return *this;
  }
};

istream& operator >> (istream &in, bign& x)
{
  string s;
  in >> s;
  x = s.c_str();
  return in;
}

ostream& operator << (ostream &out, const bign& x)
{
  out << x.str();
  return out;
}
int main() {
  bign a;
  char c[8];
  int num;
  bign b;

  while(cin>>c>>num)
  {
      cnt = r = 0;
      int i,j;
      for(i = 0;i < 6;i++)
      {
          if(c[i] == '.')
          {
              cnt = 6 - i - 1;
              for(j = i + 1;j < 6;j++)
              {
                  c[j - 1] = c[j];
              }
              c[5] = '\0';
              break;
          }
      }
      a = c;
      r = cnt;
      for(i = 1;i < num;i++)
      {
          a = a * c;
          r += cnt;
      }
      cout<<a<<endl;
  }
  return 0;
}





你可能感兴趣的:(UVa 748 - Exponentiation)