使用嵌套循环在 C 中打印星形('*')菱形? [英] Print star ('*') diamond in C with nested loops?

查看:33
本文介绍了使用嵌套循环在 C 中打印星形('*')菱形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户为钻石输入 5 时,我希望能够打印这样的钻石.但也适用于任何奇数且大于 0 的值.

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.

我有一个代码可以为用户输入 5 制作一个菱形,但不适用于所有奇数输入..

I have a code that works to make a diamond for user input of 5 but won't work for all odd number inputs..

 half = (size/2)+1;

 for (a=1; a <=  half ; a++) /*top to mid row of diamond*/
   {
     for (b=a; b<half;b++)
       {
     printf(" ");
       }
     for (c= size -2* a; c <=  half; c++)
       {
     printf("*");
       } 
      printf("
");
   }
 for (a = 1; a < half;a++)
   {
     for (b = a; b>0;b--)
       {
     printf(" ");
       }
     for (c = size-2*a; c >0 ;c--)
       {
     printf("*");
       }
     printf("
");
   }


  return 0;
}

任何帮助将不胜感激.谢谢.

Any help would be greatly appreciated.Thank you.

迈克

推荐答案

几乎可以肯定是家庭作业,所以这里有一个线索.

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.

然后你可以使用两个连续的 for 循环,一个增加星数,另一个减少它.

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.

如果您在阅读以上内容后仍然遇到问题,请考虑这一点.对于(奇怪的,正如您在评论中声明的那样)n 的输入,空间计数从 (n - 1)/2 开始,星数从 <代码>1.对于后续的每一行,空格数减少1,星数增加2.

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.

直到空间计数达到 0 为止,然后你转身走另一条路,确保你没有打印两次中间线.

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.

一旦星星数达到0,你就大功告成了.

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.

现在,由于您已经使用了自己的代码,以下是我用于概念验证的 Python 代码,出于完整性考虑:

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

如果您使用更现代的功能,您可能可以用 Python 更简洁地编写它,但这会违背快速理解和轻松翻译的目的.只需将 n 更改为您想要的任何数字(奇数且大于 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 :-)

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

这篇关于使用嵌套循环在 C 中打印星形('*')菱形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆