poj1001

简单题

先把小数点去掉(乘以10的若干次幂),按整数计算,然后再计算有多少小数位即可。

View Code
#include <iostream>
#include <string>
using namespace std;

const int w = 100000, maxn = 300, ww = 5;

string st, ansst;
int pointpos, n, fac;
long long ans[maxn];

void init()
{
    int i, x = 0;

    pointpos = 0;
    for (i = 0; i < 6; i++)
    {
        if (st[i] == '.')
        {
            pointpos = 6 - i - 1;
            continue;
        }
        x *= 10;
        x += st[i] - '0';
    }
    fac = x;
    cin >> n;
}

void mul(long long *ans, int fac)
{
    int i;
    long long jw = 0, ls;

    for (i = 1; i <= ans[0]; i++)
    {
        ls = jw + ans[i] * fac;
        ans[i] = ls % w;
        jw = ls / w;
    }
    if (jw > 0)
    {
        ans[0]++;
        ans[ans[0]] = jw;
    }
}

void work()
{
    int i;

    ans[0] = 1;
    ans[1] = 1;
    for (i = 0; i < n; i++)
        mul(ans, fac);
}

string tostring(long long x)
{
    string re;
    int i, j;

    j = w;
    for (i = 0; i < ww; i++)
    {
        j /= 10;
        re += char('0' + x / j);
        x %= j;
    }
    return re;
}

void print()
{
    int pp, i;

    ansst = "";
    for (i = ans[0]; i > 0; i--)
        ansst += tostring(ans[i]);
    while (ansst[0] == '0')
        ansst.erase(0, 1);
    pp = pointpos * n;
    if (pp > ansst.length())
        for (i = ansst.length(); i < pp; i++)
            ansst.insert(0, "0");
    if (pp > 0)
    {
        ansst.insert(ansst.length() - pp, ".");
        while (ansst[ansst.length() - 1] == '0')
            ansst.erase(ansst.length() - 1, 1);
    }
    if (ansst[ansst.length() - 1] == '.')
        ansst.erase(ansst.length() - 1, 1);
    cout << ansst << endl;
}

int main()
{
    //freopen("t.txt", "r", stdin);
    while (cin >> st)
    {
        init();
        work();
        print();
    }
}

 

posted @ 2012-11-25 14:48  金海峰  阅读(152)  评论(0编辑  收藏  举报