我想根据用户的选择为'n'行输出以下输出 [英] I want the following output for 'n' rows as per users's choice

查看:76
本文介绍了我想根据用户的选择为'n'行输出以下输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是试用..但找不到错误的输出..请说明...



我尝试过: < br $> b $ b


this is the trial.. but wrong output is found.. please explain...

What I have tried:

#include<iostream.h>
#include<conio.h>

void main()
{
  int i,j,n,count=0,k;
  clrscr();
  cin>>n; //input rows
  for(i=1;i<=n;++i)
  {
    for(j=n;j>i;j--)
    {
      cout<<" ";
    }
    for(k=0;k<2*i-1;k++)
    {
      cout<<"*";
      if(i==n)
      {
        ++count;
      }
    }
    cout<<endl;
  }

  for(i=1;i<=n-1;++i)
  {
    for(j=n;j>n-i;++j)
    {
      cout<<" ";
    }
    for(k=0;k<count-2;++k)>
    {
      cout<<"*";
    }
    cout<<endl;
  }
  getch();
}

推荐答案

这是作业,所以我不会给你任何代码!

但是......如果你考虑它并不是那么困难,并把它分解成块。

首先写一个函数来打印一行:两个参数,空格数和星数。这几乎是微不足道的!



然后查看您所需的输出:它分为两部分:

This is homework, so I'll give you no code!
But...it's not that difficult if you think about it, and break it down into chunks.
Start by writing a function to print a line: two parameters, number of spaces, and number of stars. That's almost trivial!

Then look at your required output: it's in two halves:
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *

增加星星,然后

Increasing stars, then

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

减少星星。

所以把它视为两半!

看看增加的星星部分,它实际上是三个部分:

Decreasing stars.
So treat it as two halves!
Look at the "increasing stars" section and it's really three sections:

. . . . * . . . . 
. . . * * * . . .
. . * * * * * . . 
. * * * * * * * .
* * * * * * * * *

4个房间,1个星期,4个房间。

3个房间,3个星星,3个房间。

2个空格,5个星星,2个空格。

1个空格,7个星星,1个空格。

0个空格,9个星星,0个空格。

看起来像这样,每行有九个字符,所以星数是(行号* 2)+ 1:0 * 2 + 1 = 1,1 * 2 + 1 = 3,依此类推。

总空间数为9 - 星数。

所以要绘制一条线,取行号(从零开始),加倍并加一 - 这是传递给画线功能的数字,作为星数。并且空格数是(9 - 星数)/ 2

减少星数是相同的过程,计算略有不同。



试试吧!如果你考虑它,这真的不复杂。

4 spaces, 1 star, 4 spaces.
3 spaces, three stars, 3 spaces.
2 spaces, five stars, 2 spaces.
1 space, seven stars, 1 space.
0 spaces, nine stars, 0 spaces.
Looked at like that, each line has nine characters, so the stars count is (line number * 2) + 1 : 0*2+1 = 1, 1*2+1 = 3, and so on.
And the total spaces count is 9 - the stars count.
So to draw a line, take the line number (starting from zero), double it and add one - that's the number to pass to the "draw a line" function as the "number of stars". And the "number of spaces" is (9 - number of stars) / 2
Decreasing the number of stars is the same process with slightly different calculations.

Try it! It's really not complicated if you think about it.


你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



建议:拿一张纸

注意你输入的数字
$ b图中每行$ b

- 注意行号,空格数,星数

尝试找到所有这些数字之间的关系



如果可以提供帮助,请将图纸分成两半。 />
否则函数绝对值可能有帮助

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Advice: take a sheet of paper
Note the number you input
for each line in the drawing
- note line number, number of spaces, number of stars
try to find the relation between all those numbers

If it can help, split the drawing in 2 half.
Otherwise the function absolute value may help
    X =-4 -3 -2 -1  0  1  2  3  4
abs(X)= 4  3  2  1  0  1  2  3  4


这篇关于我想根据用户的选择为'n'行输出以下输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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