POJ2346总结

Lucky tickets

题目来源

Description

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?
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 10 digits on one ticket.

Input

Input contains a positive even integer N not greater than 10. It’s an amount of digits in a ticket number.

Output

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

Sample Input

4

Sample Output

670
#include<stdio.h>
#include<string.h>
int n,ans;
int num[6][50];// num[i][j]: the cases of use i dits to get summary j 
int main(){
	ans=0;
	memset(num,0,sizeof(num));
	scanf("%d",&n);
	for (int i=0;i<=9;i++){
		num[1][i]=1;
	}
	for (int i=2;i<=n/2;i++){
		for (int j=0;j<=9*i;j++){// j is the summary this loop to form
			for (int k=0;k<=9;k++){
				if (j-k>=0){
					num[i][j]+=num[i-1][j-k];
				}
			}
		}
	}
	for (int i=0;i<=9*n/2;i++){
		ans+=num[n/2][i]*num[n/2][i];
	}
	printf("%d\n",ans);
	return 0;
}

发表评论