码迷,mamicode.com
首页 > 其他好文 > 详细

寒假集训.Lucky Tickets. Easy!

时间:2015-02-01 16:16:50      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:c++   c   acm   oj   

Lucky Tickets. Easy!
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Background

The public transport administration of Ekaterinburg is anxious about the fact that passengers don’t like to pay for passage doing their best to avoid the fee. All the measures that had been taken (hard currency premiums for all of the chiefs, increase in conductors’ salaries, reduction of number of buses) were in vain. An advisor especially invited from the Ural State University says that personally he doesn’t buy tickets because he rarely comes across the lucky ones (a ticket is lucky if the sum of the first three digits in its number equals to the sum of the last three ones). So, the way out is found — of course, tickets must be numbered in sequence, but the number of digits on a ticket may be changed. Say, if there were only two digits, there would have been ten lucky tickets (with numbers 00, 11, …, 99). Maybe under the circumstances the ratio of the lucky tickets to the common ones is greater? And what if we take four digits? A huge work has brought the long-awaited result: in this case there will be 670 lucky tickets. But what to do if there are six or more digits?

Problem

So you are to save public transport of our city. Write a program that determines a number of lucky tickets for the given number of digits. By the way, there can’t be more than nine digits on one ticket.

Input

contains a positive even integer not greater than 9.

Output

should contain a number of tickets such that the sum of the first half of digits is equal to the sum of the second half of digits.

Sample Input

input output
4
670
我的方法投机取巧了:
这是别人的代码:
#include<stdio.h>
#include<string.h>
#define MAXD 10
#define MAXS 50
const int N = 4, S = 40;
int f[MAXD][MAXS], ans[MAXD];
void prepare()
{
    int i, j, k, sum;
    memset(f, 0, sizeof(f));
    f[0][0] = 1;
    for(i = 1; i <= N; i ++)
    {
        sum = k = ans[i] = 0;
        for(j = 0; j <= S; j ++)
        {
            if(j - k > 9)
                sum -= f[i - 1][k ++];
            sum += f[i - 1][j];    
            f[i][j] = sum;
            ans[i] += f[i][j] * f[i][j];
        }
    }
}
int main()
{
    int n;
    prepare();
    while(scanf("%d", &n) == 1)
    {
        if(n & 1)
            printf("0\n");
        else
            printf("%d\n", ans[n / 2]);    
    }
    return 0;    
}


寒假集训.Lucky Tickets. Easy!

标签:c++   c   acm   oj   

原文地址:http://blog.csdn.net/a576699534/article/details/43371123

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!