POJ 3982 序列

题目

源地址:

http://poj.org/problem?id=3982

理解

题意非常清晰,唯一的问题在于大数的实现。

代码


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define debug puts("-----")
#define pi (acos(-1.0))
#define eps (1e-8)
#define inf (1<<28)
using namespace std;

#define TT 106

int a[TT], b[TT], c[TT];

void solve()
{
    int TIME = 99;
    while (TIME -- )
    {
        for (int i = 0; i < TT; ++i)
        {
            a[i] += b[i] + c[i];
            if (a[i] > 9)
            {
                a[i + 1] += a[i] / 10;
                a[i] %= 10;
            }
        }
        for (int i = 0; i < TT; ++i)
        {
            int tmp = a[i];
            a[i] = b[i];
            b[i] = c[i];
            c[i] = tmp;
        }
    }
    TIME = TT;
    while (!a[TIME--]);
    for (int i = TIME + 1; i >= 0; --i)
        printf("%d", a[i]);
    printf("\n");
}

void init(int x, int y, int z)
{
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(c, 0, sizeof(c));
    int i = 0;
    while (x > 0)
    {
        a[i++] = x % 10;
        x /= 10;
    }
    i = 0;
    while (y > 0)
    {
        b[i++] = y % 10;
        y /= 10;
    }
    i = 0;
    while (z > 0)
    {
        c[i++] = z % 10;
        z /= 10;
    }
}

int main(int argc, char const *argv[])
{
    int x, y, z;
    while (~scanf("%d%d%d", &x, &y, &z))
    {
        init(x, y, z);
        solve();
    }
    return 0;
}

更新日志

  • 2014年08月21日 已AC。
comments powered by Disqus