如何为n个模式打印这种模式(n - 第1行中的连续#s): [英] How do I print this sort of patterns for n numbers of patterns (n - no. Of consecutive #s in 1st row):

查看:95
本文介绍了如何为n个模式打印这种模式(n - 第1行中的连续#s):的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1234 (n=4)

####       ####
   ###   ###
     ## ##
       #  
     ## ##
   ###   ###
####       ####





我尝试了什么:





What I have tried:

#include<stdio.h>

main()
{
    int n,m,sp,i,j,p,k,sp1;

    printf("Enter n: "); scanf("%d",&n);
    sp1 = p = n;
    m = 2*n-1;
    sp = n-1;
    //printf("%d",sp);
    for(i=1;i<=m;i++)
    {
        if(i<n)>
        {
            if(i!=1)
            {
            for(k=1;k<=sp;k++)
                printf(" ");sp=sp+2;
            }
            for(j=0;j<p;j++)>
                printf("*");

            for(k=1;k<=sp1;k++)
                printf(" ");

            for(j=0;j<p;j++)>
                printf("*");
            p--;sp1-=2;
        }
        if(i==n)
        {
            for(k=1;k<=sp;k++)
                printf(" ");

            for(k=1;k<=p;k++)
                printf("*");

            sp-=2;p++;sp1+=3;
        }
        if(i>n)
        {
        for(k=1;k<=sp;k++)
                printf(" ");
            for(j=0;j<p;j++)>
            printf("*");
            sp=sp-2;
            p++;
        }
        printf("\n");
    }
}





这仍然不完整。



this is still incomplete.

推荐答案

通过查看模式开始,因为它比看起来更复杂。

它由行组成:(n * 2) - 其中1个,每个都镜像在1 hash行的另一面:

Start off by looking at the pattern, because it's more complex than it looks.
It's made of lines: (n * 2) - 1 of them, each of which is mirrored on the other side of the "1 hash" row:
4 hashes, 7 spaces, 4 hashes
3 spaces, 3 hashes, 3 spaces, 3 hashes, 3 spaces
5 spaces, 2 hashes, 1 spaces, 2 hashes, 5 spaces
7 spaces, 1 hashes, 7 spaces
5 spaces, 2 hashes, 1 spaces, 2 hashes, 5 spaces
3 spaces, 3 hashes, 3 spaces, 3 hashes, 3 spaces
4 hashes, 7 spaces, 4 hashes



所以请将其视为两半。打印相同但反转的东西。然后专注于让一半工作,因为一旦你完成了第一次工作,另一半是微不足道的。

现在看看线条:它们也是围绕线条中心的镜像。 br />


a 空格, b 哈希, c 空格, c 空格, b 哈希,一个空格。

再次,把它视为两半,让一半工作,另一半是微不足道。

所以模式你试图复制的是:


So treat it as two halves. which print the same thing but reversed. Then concentrate on getting one half working, because the other half is trivial once you've done the first.
Now look at the lines: They are also a mirror image around the center of the line.

a spaces, b hashes, c spaces, c spaces, b hashes, a spaces.
So again, treat it as two halves and get one half working as the other is trivial.
So the pattern you are trying to duplicate is:

           a  b  c
####       0  4  4
   ###     3  3  2
     ##    5  2  1
       #   7  1  0



a空格,b哈希,c空格。

哈希计数b很简单:


a spaces, b hashes, c spaces.
Hash count b is easy:

b = n - line number



总字符很简单:


Total characters is easy:

x = a + b + c = n * 2.



其余部分很复杂,因为模式看起来不正确:第一行是非常多的从后续行传出:a应为1,c应为3以适应开发顺序。

如果你画错了,那就很简单:


The rest is complicated, because the pattern doesn't look right: the first row is very much different from the subsequent rows: a should be 1, c should be 3 to fit the developing sequence.
If you drew it wrong, then it's simple:

c = b - 1
a = (n * 2) - b

这很容易做到:三个循环一个接一个。



但是如果你画得恰到好处,那么就不能从那个数据推断出n = 5的第一行应该是什么样子,因为它不符合序列,这很重要 - 因为如果你不能弄清楚行中的那些,你不能概括算法。你必须做出假设。

所以首先回到你的作业问题并检查它:你需要的图表:

And that's easy to do: three loops one after another.

But if you drew it right, then you can't extrapolate from that data to what the first row of n = 5 should look like because it doesn't fit the sequence, and that's important - because if you can't work out what one the the rows looks like, you can't generalize the algorithm. You have to make assumptions instead.
So start by going back to your homework question and checking it: it the diagram you need this:

####       ####
   ###   ###
     ## ##
       #  
     ## ##
   ###   ###
####       ####

或者这个:

Or this:

####     ####
  ###   ###
    ## ##
      #  
    ## ##
  ###   ###
####     ####

因为如果它是第一个,你需要更多的信息或决定更大的行需要做什么。

Because if it's the first one, you need more information or to decide what "bigger" rows need to do.


这篇关于如何为n个模式打印这种模式(n - 第1行中的连续#s):的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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