DHPC1701 - Editorial

PROBLEM LINK:

Contest

Author: Adabelle Xie

DIFFICULTY:

CAKEWALK

PREREQUISITES:

None

PROBLEM:

Given a number N and a string S, print out a line that contains N space separated repetitions of S.

QUICK EXPLANATION:

Use a loop to repeat a print statement printing S and a space N-1 times. Then print S (with no space).

EXPLANATION:

Since we need N copies of S to be printed, naturally we will want to use a loop. We need the spaces between the copies of S, but not any leading or trailing spaces. Therefore, the last copy of S cannot have a space behind it. If we run the print statement the full N times printing S + “ “, the last S will have a space. Therefore, we want to run the print statement N-1 times, and include a print statement after that only prints S.

ALTERNATIVE SOLUTION:

You can also include a conditional inside your loop to check when you are printing the last S. Inside the body of the conditional, include the print statement printing only S.

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.