我该如何编写此代码?

lok*_*esh -1 c c++

我必须编写一个程序来输出以下文本(不多也不少):

   1 
  12
 123
1234
Run Code Online (Sandbox Code Playgroud)

作为初学者,我完全陷入了困境.解决这个问题的最佳方法是什么?我应该使用什么算法?

pax*_*blo 6

这里有一些伪代码可以帮助您入门.我没有为明显的学习/家庭作业问题提供真实的代码,因为从长远来看,这对你没有好处.

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.
Run Code Online (Sandbox Code Playgroud)

你的工作是实际运行你的程序,在你去的时候记下变量的内容并记录输出的内容.

然后,一旦您完全理解它的工作原理和原因,请将其转换为您选择的语言.看,这是一张让你凝视的表:

  i      j    output
-----  -----  ------
  1
Run Code Online (Sandbox Code Playgroud)

  • +1你把它钉在了头上,然后把它钉上了.这应该让他继续前进. (2认同)