Print star ('*') diamond in C with nested loops?

前端 未结 10 1755
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 14:59

I want to be able to print a diamond like this when the user enters 5 for the diamond. But also will work for any value that is odd and greater than 0.

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-07 15:38

    Almost certainly homework so here's a clue.

    Count the number of spaces before a line and the number of stars in that line. What you're looking for is a relationsip between the line number and those two values.

    Then you can use two consecutive for loops, one increasing the star count and the other decreasing it.

    Within each of those loops would be two more consecutive loops to print out the required number of spaces followed by the required number of stars followed by a newline character.


    If you're still having trouble after reading the above, consider this. For an input of (odd, as you state you enforce in your comments) n, the space count starts at (n - 1) / 2 and the star count at 1. For each subsequent line, the space count reduces by 1 and the star count increases by 2.

    That works up until the point where the space count reaches 0, then you turn around and go the other way, making sure you don't print that middle line twice.

    Once the star count reaches 0, you're done.

    Now you just have to turn that specification into code :-)


    Now, since you've indicated in comments that you're interested in making your own solution rather than just being handed code, I feel comfortable giving you something you can check your own solution against. Here's the pseudo-code I would use:

    # Input data, check and init counters.
    
    input n
    make sure n is odd and greater than 2
    set numspaces to (n-1) / 2
    set numstars to 1
    
    # Gradually get wider until just before middle line.
    
    while numspaces > 0:
        for i = 1 to numspaces: output " "
        for i = 1 to numstars:  output "*"
        output newline
        subtract 1 from numspaces
        add 2 to numstars
    
    # Gradually get thinner until end.
    
    while numstars > 0:
        for i = 1 to numspaces: output " "
        for i = 1 to numstars:  output "*"
        output newline
        add 1 to numspaces
        subtract 2 from numstars
    

    And, as a final exercise, you can refactor:

        for i = 1 to numspaces: output " "
        for i = 1 to numstars:  output "*"
        output newline
    

    into a separate function, since it's common between the two loops.


    And now, since you've got your own code working, here's the Python code I used for proof of concept, included for completeness:

    def lineout (sp, st):
        s = ""
        for i in range (sp): s = "%s "%(s)
        for i in range (st): s = "%s*"%(s)
        print s
    
    n = 21
    numspaces = (n-1) / 2
    numstars = 1
    
    while numspaces > 0:
        lineout (numspaces, numstars)
        numspaces -= 1
        numstars += 2
    
    while numstars > 0:
        lineout (numspaces, numstars)
        numspaces += 1
        numstars -= 2
    

    You could probably write it more succinctly in Python if you used the more modern features but that would rather defeat the purpose of quick understanding and easy translation. Just change n to whatever number you want (odd and greater than two, providing the resultant diamond will fit in your terminal) and enjoy the output :-)

              *
             ***
            *****
           *******
          *********
         ***********
        *************
       ***************
      *****************
     *******************
    *********************
     *******************
      *****************
       ***************
        *************
         ***********
          *********
           *******
            *****
             ***
              *
    

提交回复
热议问题