E - Lucky Tickets. Easy!
Time Limit:2000MS Memory Limit:16384KB
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 |
Solution:一开始被题目欺骗了,原本说前三位数字之和等于后三位数字之和的票为Lucky Ticket,原来是前一半数字之和和后一半数字之和
相等的票才是。 由于输入数字为偶数,且小于9,时间内存存足,直接强破。
相等的票才是。 由于输入数字为偶数,且小于9,时间内存存足,直接强破。
#include<iostream>
using namespace std;
void f2() {
int count = 0;
for (int i = 0; i <= 9; i++)
for (int j = 0; j <= 9; j++)
if (i == j)
count++;
cout << count << endl;
}
void f4() {
int count = 0;
for (int i = 0; i <= 9; i++)
for (int j = 0; j <= 9; j++)
for (int k = 0; k <= 9; k++)
for (int l = 0; l <= 9; l++)
if (i + j == k + l)
count++;
cout << count << endl;
}
void f6() {
int count = 0;
for (int i = 0; i <= 9; i++)
for (int j = 0; j <= 9; j++)
for (int k = 0; k <= 9; k++)
for (int l = 0; l <= 9; l++)
for (int m = 0; m <= 9; m++)
for (int n = 0; n <= 9; n++)
if (i + j + k == l + m + n)
count++;
cout << count << endl;
}
void f8() {
int count = 0;
for (int i = 0; i <= 9; i++)
for (int j = 0; j <= 9; j++)
for (int k = 0; k <= 9; k++)
for (int l = 0; l <= 9; l++)
for (int m = 0; m <= 9; m++)
for (int n = 0; n <= 9; n++)
for (int o = 0; o <= 9; o++)
for (int p = 0; p <= 9; p++)
if (i + j + k + l == m + n + o + p)
count++;
cout << count << endl;
}
int main() {
int n;
cin >> n;
if (n == 2)
f2();
else if (n == 4)
f4();
else if (n == 6)
f6();
else if (n == 8)
f8();
return 0;
}