C++ 嵌套 For 循环

标签 c++ for-loop visual-c++ nested-loops

我认为这会比最初计划的更容易。我试图用嵌套的 for 循环制作这张图片:

enter image description here

任何建议或解决方案都会有所帮助。

#include <iostream>

using namespace std;

int main()
{
    for(int i=0; i<1;i++)
    {
        cout<<i+1<<endl;

        for(int j=0;j<2;j++)
        {
            cout<<j+1;
        }
     }

    cout<<"\n";
    for(int k=0; k<3; k++)
    {
        cout<<k+1;
    }
    cout<<"\n";
    for(int l=0; l<4; l++)
    {
        cout<<l+1;
    }
    cout<<"\n";
    for(int m=4; m>0; m--)
    {
        cout<<m;
    }
    cout<<"\n";
    for(int n=3; n>0; n--)
    {
        cout<<n;
    }
    cout<<"\n";
    for(int o=2; o>0; o--)
    {
        cout<<o;
    }
    cout<<"\n";
    for(int p=0; p<1; p++)
    {
        cout<<p+1;
    }
    cin.get();

    return 0;
}

最佳答案

这里有一个 C 语言的解决方案 =)

#include <stdio.h>
#include <string.h>

int main(void) {
    char forward[5] = "1";
    char reverse[5] = "4321";

    int i;
    for( i = 1; i <= 4; i++ ) {
        printf( "%s\n", forward );
        forward[i] = forward[i-1]+1;
    }
    for( i = 0; i < 4; i++ ) printf( "% 4s\n", reverse+i );
}

关于C++ 嵌套 For 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14888238/

相关文章:

c++ - 你如何在 C++ 中的 priority_queue 中排序对象?

c++ - 编译 Sundance-4.73

python - 我如何定义一个函数,如果列表中的所有项目都不同则返回 False,否则返回 True

Java - 如何在同一行上循环和打印来自 2 个 HashMap 的值?

for-loop - 批处理,如何执行存储在变量或txt文件中的代码?

arrays - 具有巨大多维数组的 malloc,无法用于神经科学模拟

visual-c++ - Visual Studio 中的 "Win32 Project"、 "CLR Empty Project"和 "Empty Project"模板有什么区别?

c++ - C++如何从文件中读取数据

c++ - 如何在 C++ 中正确存储正则表达式匹配

c++ - 找不到 bsearch(vc2008) 甚至包括头文件