一行中的星星图案 [英] Star Patterns in One Line

查看:90
本文介绍了一行中的星星图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是我的作业而且由于某种原因而被分配了,但是我已经在这个问题上待了一个星期但我仍然没有想到它。



所以这里的目标是使用一行C ++代码创建四种模式(行数基于用户输入)。这四种模式是:

I know this is my homework and it is assigned for a reason, but I've been sitting on the problem for a week and I still haven't figured it out.

So the goal here is to create four patterns (amount of rows is based on user input) using one line of C++ code. The four patterns are:

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





我已经弄清楚如何编码每个模式,但这样做意味着我有两个/三个while循环。我们还没有在这个类中学习for循环,因此不允许使用它。但我不知道如何将这些循环压缩成一行!如果我作弊并使用for循环,它几乎适用于第一和第四种模式。但是否则......



这是我对模式的代码(n是用户输入的行数):



模式1:



I've figured out how to code each pattern, but doing so means that I have two/three while loops. We haven't learned the for loop in this class and are therefore not allowed to use it. But I have no idea how I can even compress those loops into one single line! If I cheat and use for loops it almost works for the first and fourth patterns. But otherwise...

Here's the code I have for the patterns (n is user input of amount of rows):

Pattern 1:

int i, j, n;
i = 1;
while (i <= n) {
    j = 0;
    while (j < i) {
        cout << "*";
        j++;
    }
    cout << endl;
    i++;
}





模式2:



Pattern 2:

int i, j, k, n;
i = 0;
while (i < n) {
    k = 0;
    while (k < i) {
        cout << " ";
        k++;
    }
    j = n;
    while (j > i) {
        cout << "*";
        j--;
    }
    cout << endl;
    i++;
}





模式3:



Pattern 3:

int i, j, k, n;
i = 1;
while (i <= n) {
    k = n;
    while (k > i) {
        cout << " ";
        k--;
    }
    j = 0;
    while (j < i) {
       cout << "*";
       j++;
    }
    cout << endl;
    i++;
}





模式4:



Pattern 4:

int i, j, n;
i = 0;
while (i < n) {
    j = n;
    while (j > i) {
        cout << "*";
        j--;
    }
    cout << endl;
    i++;
}





我们在课堂上学到的是非常基础的,因为本课程是对计算科学的介绍。它应该触及内部的许多不同主题,而不会深入探讨。我们为C ++学到的只是基本声明,if语句和while循环。



我真的需要帮助。 :(一些提示或提示会很适合我;我正在寻求帮助,而不是有人为我做我的工作。



提前谢谢!



What we learned in class is very basic because this course is an introduction to computing science. It is supposed to touch on a lot of the different topics within in without going into too much depth. All we really learned for C++ is basic declaration, if statements, and the while loop.

I really need help. :( Some tips or hints will suit me just fine; I'm asking for help, not for someone to do my work for me.

Thank you in advance!

推荐答案

好吧,我误解了我的老师。:(非常抱歉,大家!他希望所有的图案都印在一行,不一定是一行代码。那些感兴趣的人,这是我结束的代码(并且他接受了解决方案):

Okay, I misunderstood my teacher. :( Terribly sorry, everyone! He wanted all of the patterns printed in one line, not necessarily one line of code. For those who are interested, here's the code I ended with (and that he accepted as a solution):
int i, j, k, n;

cout << "Enter number of rows: ";
cin >> n;

i = 0;
while (i < n) {
    
    j = 0;
    while (j <= i) {
        cout << "*";
        j++;
    }

    k = n - 1;
    while (k > i) {
        cout << " ";
               k--;
    }

    cout << "     ";

    k = 0;
    while (k < i) {
        cout << " ";
        k++;
    }

    j = n;
    while (j > i) {
        cout << "*";
        j--;
    }

    cout << "     ";

    j = n - 1;
    while (j > i) {
        cout << " ";
        j--;
    }

    k = i;
    while (k >= 0) {
        cout << "*";
        k--;
    }

    cout << "     ";

    j = n;
    while (j > i) {
        cout << "*";
        j--;
    }

    cout << endl;
    i++;
}





再一次,抱歉我可能引起的头痛。 ^^



Once again, sorry for the headaches I may have caused. ^^


这应该让你开始,但你需要为自己找出正确的间距值

This should get you started, but you will need to figure out the correct spacing values for yourself
int i = 0;
while (i != 3)
    cout << string(i, '*') << string(4-i, ' ') << setw(4) << string(4 - i, '*') << setw(6) << string(i, '*')  << string(4, ' ') << string(4 - i, '*') << string(i++, ' ') << endl;


多么大脑弯曲。我可以在这里看到一个模式,我现在还不能完全改变它。



看看你的例子,我记下了星号和空格的数量在每一行。这给了我下表:



What a brain-bender. I can see a pattern here, I just can't quite paramaterize it any better just yet.

Looking at your example, I noted down the number of asterisks and spaces on each line. This gave me the following table:

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

1, 5, 3, 7, 1, 5, 3
2, 5, 2, 6, 2, 5, 2
3, 5, 1, 5, 3, 5, 1





你可以清楚地看到奇怪的关系-numbered列到最大行数,N。

您还可以看到偶数列有自己的模式 - n + 1 + 2,2 *(n + 1) -row,n + 1 + 2 N + 2,2 *(N + 1)-row,N + 2



我把一个版本砸在了一起使用for循环,然后重新阅读你的问题。所以我将它们转换为while循环。显然,我使用了超过1行代码。但是,这样做 - 我注意到每行都有一个重新出现的模式。第一行中的1,5,3重复,第二行中的2,5,2重复。什么是大脑破坏者!



这是一些值得考虑的颂歌。也许你可以找出一种我还没有完全掌握的模式。这种模式可以消除我所展示的大部分重复,并且这样做会产生(几乎/实际上)合理的代码作为单行代码。



我测试的N范围高达约25,并且获得相同的奖励,尽管规模更大。请注意,为了简化/简化每个while循环的终止条件,N的数字大于您实际想要打印的行数。





You can clearly see a relationship in the odd-numbered columns to the max number of rows, N.
You can also see that the even numbered columns have their own patterns - n+1+2, 2*(n+1)-row, n+1+2 N+2, 2*(N+1)-row, N+2

I smashed together a version using for loops, then re-read your question. So I converted them to while loops. Clearly, I'm using more than 1 line of code. But, in doing so - I noticed that there was a re-occurant pattern along each row. 1,5,3 in the first row is repeated, as is 2,5,2 in the second. What a total brain buster!!

Here's some ode to consider. Perhaps you can identify a pattern I've just not quite come to grips with yet. A patter that would eliminate much of the repetition I've exhibited and in doing so, produce code that is (almost/actually) reasonable as a single line of code.

I tested with N ranging up to about 25 and was rewarded with the same pattern, albeit scaled ever larger. Note that for the ease/brevity of the terminating condition of each while loop, N holds a number 1 larger than the number of rows you'd actually like to print.

int main()
{
    int row, sp, st;
    int n = 4;

    row=1; while (row < n)
    {
        st=0; while(st++ < row) printf("*");
        sp=0; while(sp++ < n+1) printf(" ");
        st=0; while(st++ < n-row) printf("*");
        sp=0; while(sp++ < 2*n-row) printf(" ");

        //---------------
        st=0; while(st++ < row) printf("*");
        sp=0; while(sp++ < n+1) printf(" ");
        st=0; while(st++ < n-row) printf("*");

        printf("\n");
        row++;
    }
    return 0;
}





这是(我的)n = 10的样本



Here's a sample of (my)n = 10

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





我的头疼!



My head hurts!


这篇关于一行中的星星图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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