Write pseudocode of a program that prints such a pattern

淺唱寂寞╮ 提交于 2019-12-12 19:56:28

问题


Analyze output pattern and write algorithm of a program that prints such a pattern.

Input 4
Pattern:

55555
4444
333
22
1

Input 3
Pattern:

333
22
1

Process (what I have come up with)

n  = input (“Enter a positive integer”)
r= 0
while r < n 
    c = (n – r) + 1
    while c > 0
        s = n – r
        print s 
        c = c – 1
    end
    r = r + 1
    n = n – 1
    print end l
end

Problem: I have used r for rows, and c for columns. The problem rises in c = (n – r) + 1 for first row. It makes the first row n+1, works for following rows. On dry run i get

Input 3
Pattern:
444
22
1


回答1:


This should work:

n = input (“Enter a positive integer”)
while n > 0 
    c = n
    while c > 0
        print n
        c = c – 1
    end
    n = n - 1
    print end l
end

Be careful about what meaning you give to your variables and therefore, how you treat them consitently ;)




回答2:


why are you using while for something that is an obviously an example of for statement?

n  = input (“Enter a positive integer”)

for(i=n ; i > 0 ; i--)
{
   for(j=0 ;j<i; j++)
   {
     print i;
   }

   print "\n";
}


来源:https://stackoverflow.com/questions/18812543/write-pseudocode-of-a-program-that-prints-such-a-pattern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!