程序code字形垂直矩阵 [英] Program to code zigzag matrix vertically

查看:165
本文介绍了程序code字形垂直矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个动态code打印的N×n矩阵在锯齿形图案。请帮我用code得到的输出如下所述:

在code,我已经与Rizier123帮助到目前为止已经试过在水平锯齿形:

 的#include<&stdio.h中GT;诠释主(){    INT行,列;
    INT rowCount时,列数,计数= 0;    的printf(请输入的行和列:\\ n>中);
    scanf函数(%D,&安培;行&安培;列);
    为(rowCount时= 0; rowCount等&下;行; rowCount等++){        为(列数= 1;列数< =列;列数++){            如果(计数%2 == 0)
                的printf(%4D,(列数+(rowCount时*列)));
            其他
                的printf(%4D,((rowCount等+ 1)*列)-columnCount + 1);        }
        算上++;
        的printf(\\ n);
    }
    返回0;}

输入:

  5

输出:

  1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25

我想同样的锯齿形输出,但垂直..

修改

期望的输出:

  1 10 11 20 21 30
2 9 12 19 22 29
3 8 13 18 23 28
4 7 14 17 24 27
5 6 15 16 25 26


解决方案

这应该为你工作:

 的#include<&stdio.h中GT;诠释主(){    INT行,列;
    INT rowCount时,列数;    的printf(请输入的行和列:\\ n>中);
    scanf函数(%D,&安培;行&安培;列);
    为(rowCount时= 0; rowCount等&下;行; rowCount等++){        为(列数= 0;&列数LT;列,列数++){          如果(列数%2 == 0)
                的printf(%4D,行*(列数)+ rowCount等+ 1);
            其他
                的printf(%4D,(行*(列数+1)) - rowCount等);        }        的printf(\\ n);
    }
    返回0;}

输入:

  5

输出:

  1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25

I am writing a dynamic code that print n x n matrix in a zigzag pattern. Please help me with the code to get the output stated below:

The Code that I've tried so far with help of Rizier123 is in horizontal zigzag pattern:

#include <stdio.h>

int main() {

    int rows, columns;
    int rowCount, columnCount, count = 0;

    printf("Please enter rows and columns:\n>");
    scanf("%d %d", &rows, &columns);


    for(rowCount = 0; rowCount < rows; rowCount++) {

        for(columnCount = 1; columnCount <= columns; columnCount++) {

            if(count % 2 == 0)
                printf("%4d " , (columnCount+(rowCount*columns)));
            else
                printf("%4d " , ((rowCount+1)*columns)-columnCount+1);

        }
        count++;
        printf("\n");
    }


    return 0;

}

Input:

5 5

Output:

 1  2  3  4  5
10  9  8  7  6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25 

I want the same zigzag pattern output but vertically..

EDIT

Expected output:

1 10 11 20 21 30
2  9 12 19 22 29
3  8 13 18 23 28
4  7 14 17 24 27
5  6 15 16 25 26

解决方案

This should work for you:

#include <stdio.h>

int main() {

    int rows, columns;
    int rowCount, columnCount;

    printf("Please enter rows and columns:\n>");
    scanf("%d %d", &rows, &columns);


    for(rowCount = 0; rowCount < rows; rowCount++) {

        for(columnCount = 0; columnCount < columns; columnCount++) {

          if(columnCount % 2 == 0)
                printf("%4d " , rows*(columnCount)+rowCount+1);
            else
                printf("%4d " , (rows*(columnCount+1))-rowCount);    

        }

        printf("\n");
    }


    return 0;

}

Input:

5 5

Output:

1  10  11  20  21
2   9  12  19  22
3   8  13  18  23
4   7  14  17  24
5   6  15  16  25

这篇关于程序code字形垂直矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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