开发者

How do I go about writing this code? [closed]

开发者 https://www.devze.com 2023-02-09 19:39 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago开发者_StackOverflow社区.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消