Find the number of lucky tickets with the sum of the first three digits n. Ticket is called lucky if it is a
six-digit number and the sum of the first three digits equals to the sum of the
last three digits.
Input. One positive integer n (n
≤ 27).
Output. Print the number of required lucky tickets.
Sample input |
Sample output |
1 |
9 |
full search
Compute the amount p of
three-digit numbers (that can start
from zero), which sum of digits is n. If you append any three-digit number (which sum of digits is n) next to
any other three-digit
number (which sum of digits is n), you get a lucky ticket. The number of lucky tickets
is p2.
Example
Consider the case when n = 1. Three-digit numbers which sum of
digits is 1, are 001, 010 and 100. If you combine any of them with any
other, you’ll get 9 lucky
tickets.
Read the value of n.
scanf("%d",&n);
Compute in the variable p the count of three-digit numbers with the sum of digits n.
p = 0;
Iterate over the digits of a
three-digit number . If the sum
of its digits is n, then increment p by 1.
for(i = 0; i <= 9; i++)
for(j = 0; j <= 9; j++)
for(k = 0; k <= 9; k++)
if (i + j + k == n) p++;
Compute and print the answer.
res = p * p;
printf("%d\n",res);
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner con = new Scanner(System.in);
int n = con.nextInt();
int p = 0;
for(int i = 0; i <= 9; i++)
for(int j = 0; j <= 9; j++)
for(int k = 0; k <= 9; k++)
if (i + j + k == n) p++;
int res = p * p;
System.out.println(res);
con.close();
}
}
Python realization
Read the value of n.
n = int(input())
Compute in the variable p the count of three-digit numbers with the sum of digits n.
p = 0
Iterate over the digits of a
three-digit number . If the sum
of its digits is n, then increment p by 1.
for i in range(10):
for j in range(10):
for k in range(10):
if (i + j + k == n):
p += 1
Compute and print the answer.
res = p * p;
print(res)