打印星号(“*”)的钻石用C与嵌套循环? [英] Print star ('*') diamond in C with nested loops?

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

问题描述

我希望能够当用户为钻石进入5打印钻石这样。但也因为这是奇数和大于0的任何值工作。

我有一个code,致力于使为5用户输入的钻石,但不会对所有奇数投入工作..

  =一半(尺寸/ 2)+1; 用于:(a = 1;&下; =一半;一个++)/ *顶端至中期金刚石*的行/
   {
     对于(B = A,B<一半; b ++)
       {
     的printf();
       }
     为(C =尺寸-2 * A; C< =一半; C ++)
       {
     的printf(*);
       }
      的printf(\\ n);
   }
 用于:(a = 1;&所述;半;一个++)
   {
     对于(B = A,B> 0; b--)
       {
     的printf();
       }
     为(C =大小-2 *一个c取代; 0; C--)
       {
     的printf(*);
       }
     的printf(\\ n);
   }
  返回0;
}

任何帮助将大大AP preciated.Thank你。<​​/ P>

迈克


解决方案

几乎可以肯定的功课所以这里有一个线索。

计数的空格前行的数量和在该行数星星。什么你要找的是行号和这两个值之间的relationsip。

这时你可以用两个连续的循环,增加一星数和其他减少它。

的每一个这些循环将是两个更多的连续循环打印出所需数量的空格后面所需的众多明星后跟一个换行符。


如果你还在看完上面有问题,考虑这一点。对于输入(奇怪,因为你的国家,你在你的意见执行) N ,空间计数开始于(N - 1)/ 2 1 。对于每一个后续行,空间计数由 1 减少和 2 恒星数量的增加。

这是工作,直到地步空间计数达到 0 ,那么你转身走另一条路,确保你没有打印中间线的两倍

在星数达到 0 ,你就大功告成了。

现在你只需要打开该规范为code: - )


现在,因为你已经在你感兴趣的制作自己的解决方案,而不是仅仅被交给code评论中表示,我很放心给你的东西你可以核对自己的解决方案。这里的伪code,我会用:

 #输入数据,检查和init计数器。输入N
确保n是奇数和大于2
设置numspaces到(n-1)/ 2
集numstars 1#中间线才逐渐得到更广泛的,直到而已。而numspaces&GT; 0:
    对于i = 1至numspaces:输出,
    对于i = 1至numstars:输出*
    输出换行符
    从numspaces减1
    加2 numstars#渐渐地变得越来越薄,直到结束。而numstars&GT; 0:
    对于i = 1至numspaces:输出,
    对于i = 1至numstars:输出*
    输出换行符
    加1 numspaces
    从numstars减去2

和,作为最后的练习,你可以重构:

 对于i = 1到numspaces:输出
    对于i = 1至numstars:输出*
    输出换行符

到一个单独的功能,因为它是两个循环之间的共同。


而现在,既然你已经有了自己的code工作,这里的Python的code口用于概念证明,为了保持完整性:

  DEF线路输出(SP,ST):
    S =
    因为我在范围(SP):S =%​​S%(S)
    因为我在范围(ST):S =%​​S *%(S)
    打印■N = 21
numspaces =(N-1)/ 2
numstars = 1而numspaces&GT; 0:
    线路输出(numspaces,numstars)
    numspaces - = 1
    numstars + = 2而numstars&GT; 0:
    线路输出(numspaces,numstars)
    numspaces + = 1
    numstars - = 2

您也许可以更简洁地写在Python,如果你使用了更现代的功能,但该宁愿击败的快速理解和容易翻译的目的。只要改变 N 任何你想要的号码(奇数和大于二,提供最终的钻石会适合你的终端),享受输出: - )

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

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.

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("\n");
   }
 for (a = 1; a < half;a++)
   {
     for (b = a; b>0;b--)
       {
     printf(" ");
       }
     for (c = size-2*a; c >0 ;c--)
       {
     printf("*");
       }
     printf("\n");
   }


  return 0;
}

Any help would be greatly appreciated.Thank you.

Mike

解决方案

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 :-)

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

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

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