4101. Three digit numbers

 

Find all three-digit numbers with sum of digits equals to n.

 

Input. One integer n (0 ≤ n ≤ 28).

 

Output. Print in the first line the number of found three-digit numbers. In the next lines print these numbers in increasing order.

 

Sample input

Sample output

3

6

102

111

120

201

210

300

 

 

SOLUTION

loops

 

Algorithm analysis

Iterate over the digits of hundreds, tens and ones and find all three-digit numbers, which sum of digits is n.

 

Algorithm realization

Read the value of n.

 

scanf("%d",&n);

res = 0;

 

Iterate through the digits i, j, k of three-digit number and count the number of triples (i, j, k), which sum is n. The number of hundreds i cannot be zero, therefore 1 ≤ i ≤ 9.

 

for(i = 1; i <= 9; i++)

for(j = 0; j <= 9; j++)

for(k = 0; k <= 9; k++)

  if (i + j + k == n) res++;

 

Print the number of found three-digit numbers.

 

printf("%d\n",res);

 

Again iterate over the digits of hundreds, tens and ones and print all three-digit numbers, which sum of digits is n.

 

for(i = 1; i <= 9; i++)

for(j = 0; j <= 9; j++)

for(k = 0; k <= 9; k++)

  if (i + j + k == n) printf("%d%d%d\n", i, j, k);

 

Java realization

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

 

    int i, j, k, res = 0;

    for(i = 1; i <= 9; i++)

    for(j = 0; j <= 9; j++)

    for(k = 0; k <= 9; k++)

      if (i + j + k == n) res++;

 

    System.out.println(res);

 

    for(i = 1; i <= 9; i++)

    for(j = 0; j <= 9; j++)

    for(k = 0; k <= 9; k++)

      if (i + j + k == n) System.out.printf("%d%d%d\n", i, j, k);

    con.close();

  }

}

 

Python realization

 

n = int(input())

 

res = 0;

for i in range(1,10):

  for j in range(0, 10):

    for k in range(0, 10):

      if (i + j + k == n): res = res + 1;

 

print(res);

 

for i in range(1,10):

  for j in range(0, 10):

    for k in range(0, 10):

      if (i + j + k == n): print(str(i) + str(j) + str(k));