UVA 10198 Counting

Counting

The Problem

Gustavo knows how to count, but he is now learning how write numbers. As he is a very good student, he already learned 1, 2, 3 and 4. But he didn't realize yet that 4 is different than 1, so he thinks that 4 is another way to write 1. Besides that, he is having fun with a little game he created himself: he make numbers (with those four digits) and sum their values. For instance:

132 = 1 + 3 + 2 = 6
112314 = 1 + 1 + 2 + 3 + 1 + 1 = 9 (remember that Gustavo thinks that 4 = 1)
After making a lot of numbers in this way, Gustavo now wants to know how much numbers he can create such that their sum is a number n. For instance, for n = 2 he noticed that he can make 5 numbers: 11, 14, 41, 44 and 2 (he knows how to count them up, but he doesn't know how to write five). However, he can't figure it out for n greater than 2. So, he asked you to help him.

The Input

Input will consist on an arbitrary number of sets. Each set will consist on an integer n such that 1 <= n <= 1000. You must read until you reach the end of file.

The Output

For each number read, you must output another number (on a line alone) stating how much numbers Gustavo can make such that the sum of their digits is equal to the given number.

Sample Input

2
3

Sample Output

5
13

题意:Gustavo数数时总是把1和4搞混,他认为4只是1的另外一种写法。给出一个整数n,Gustavo想知道有多少个数的数字之和恰好为n。例如,当n=2时,有5个数:11、14、41、44、2。

分析:假设 F(n) 表示使用 1,2,3,4 构建的和为 n 的序列总数,则这些序列中,以 1 为开始的序列种数为 F(n - 1),以2为开始的为 F(n - 2),以3开始的序列总数为 F(n - 3)、以4开始的序列总数为  F(n - 4),由于 Gustavo 把 4 当作 1,则有 F(n - 4) = F(n - 1),
 故 F(n) = F(n - 1) + F(n - 2) + F(n - 3) + F(n - 4) = 2 * F(n - 1) + F(n - 2) + F(n - 3)。

边界条件: F(1) = 2, F(2) = 5, F(3) = 13。
#include<string>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

vector<string> v;

string add(string a, string b)
{
    string s;
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    int i = 0;
    int m, k = 0;
    while(a[i] && b[i])
    {
        m = a[i] - '0' + b[i] - '0' + k;
        k = m / 10;
        s += (m % 10 + '0');
        i++;
    }
    if(i == a.size())
    {
        while(i != b.size())
        {
            m = k + b[i] - '0';
            k = m / 10;
            s += m % 10 + '0';
            i++;
        }
        if(k) s += k + '0';
    }
    else if(i == b.size())
    {
        while(i != a.size())
        {
            m = k + a[i] - '0';
            k = m / 10;
            s += m % 10 + '0';
            i++;
        }
        if(k) s += k + '0';
    }
    reverse(s.begin(), s.end());
    return s;
}

void solve()
{
    v.push_back("0");
    v.push_back("2");
    v.push_back("5");
    v.push_back("13");
    string s;
    for(int i = 4; ; i++)
    {
        s = add(v[i-1], v[i-1]);
        s = add(v[i-2], s);
        s = add(v[i-3], s);
        v.push_back(s);
        if(v[i].size() > 1001) break;
    }
}

int main()
{
    solve();
    int n;
    int Size = v.size();
    while(cin >> n)
    {
        cout << v[n] << endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值