I have to write a program to output the following text (no more, no less):
1
12
123
1234
Being a beginner, I'm totally stuck. What is the best way to approach this? What algorithm should I use?
Here's some pseudo-code to get you started. I don't give real code for obvious learning/homework questions since it does you no good in the long term.
for i = 1 to 4: # four lines.
for j = 1 to 4-i: # each line starts with some spaces.
output a space
for j = 1 to i: # followed by the digits.
output j
output new line # move to next line.
Your job is to actually run that program in your head, writing down the contents of variables as you go and recording what gets output.
Then, once you fully understand how and why it works, convert it to your language of choice. Look, here's a sheet to get you stared:
i j output
----- ----- ------
1
精彩评论