简体   繁体   中英

C++ Nested For Loops

I thought this would be easier than originally planned. I'm trying to make this image out of nested for loops:

在此处输入图片说明

Any suggestions or solutions would be helpful.

#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;
}

Here's a solution in C for you =)

#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 );
}

If you want an extremely compact and not very follow-able solution I decided to give it a shot.

int length = 4;

for(int i = 0; i < 2; i++) 
{
    for(int j = 0; j < length; j++) 
    {
        for(int k = 0; k < (i == 1 ? length - j : j + 1); k++) 
        {
            if(i == 1 && k == 0) 
                for(int x = 0; x < j; x++)
                    cout << " ";

            cout << (i == 1 ? (length - k) - j : k + 1);
        }

        cout << endl;
    }
}

Where length is the number of iterations from 1 to length .

输出量

Ok, since paddy posted his C solution (and someone said "compact" =P)...

#include <stdio.h>
int main()
{
    char line[] = "1234321";
    int i=0;
    for (; i<4; printf("%.*s\n",++i,line));
    for (i=0;i<4; printf("%4s\n",line+3+i++));
    return 0;
}

Output

1
12
123
1234
4321
 321
  21
   1
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 /*first half: From level1 = 1 to less than 5, level2 = 1 to less than or equal to level1, print level2 */
 for(int level1 = 1; level1 < 5; level1++) { 
     for(int level2 = 1; level2 <= level1; level2++){
        cout << level2;
     }
     cout << '\n'; /*prints the newline AFTER each iteration of the `level2` loop*/
 }

 /*second half: reverse the logic of part1, but also add spaces in the beginning */
 for(int level1 = 4; level1 > 0; level1--){
     for(int interim = 4; interim > level1; interim--) cout << ' ';
     for(int level2 = level1; level2 > 0; level2--){
         cout << level2;
     }
     cout << '\n';
 }
 return 0;  
}

Let me know if you do not understand this code :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM