Print Zigzag Array

Source: Internet
Author: User

It took a lot of time today to study how to print a zigzag zigzag matrix, referring to the next Http://[email protected]/blog/static/4741291720117842634276/, Specific ideas and procedures are below.

Test2.cpp: Defines the entry point of the console application. date:2015 June 17//author:jsalienzy/* 0 1 5 6 14 15 27 28 * 2 4 7 13 16 26 29 42 * 3 8 12 17 2 5 30 41 43 * 9 11 18 24 31 40 44 53 * 10 19 23 32 39 45 52 54 * 20 22 33 38 46 51 55 60 * 21 34 3 7. * All-in-a-*///a[0][0],a[0][1],a[1][0],a[2][0],a[1][1],a[0][2],a[0][3]//zigzag row Column order//Specific ideas://1. Set up a dynamic two-dimensional array to store the data//2. Divide the matrix into upper and lower two halves, upper left and lower right half of the//3. The upper left section is traversed by rows, divided into odd and even columns, and the odd columns are successively reduced, and the singular column increments sequentially (in order)//4. The lower right half of the area is also traversed by line, odd from bottom to top, and then to the bottom by decreasing # include "stdafx.h" #include <iostream> #include <iomanip>using namespace Std;int _tmain (int argc, _tchar* argv[]) {int **data;//a n*n matrix, using dynamic array int n;cout<< "Please enter the number of rows in the N*n matrix:";cin>>n; data=new int *[n];for (int i=0; i<n; i++) {data[i]=new int [n];} Initializes a two-dimensional matrix for (int i=0; i<n; i++) {for (int j=0; j<n; J + +) {data[i][j]=0;}} int X,y;int count=0;//left upper half area for (int i=0; i<n; i++) {if (i%2==0) {x=i;y=0;for (int j=0; j<i+1; J + +) {data[x--][y++]= count++;}} if (i%2==1) {x=0;y=i;for (int j=0; j<i+1; J + +) {data[x++][y--]=count++;}}} Lower right section for (int i=1; i<n; i++) {if (i%2==0) {x=i;y=n-1;for (int j=0; j<n-i; J + +) {data[x++][y--]=count++;}} if (i%2==1) {x=n-1;y=i;for (int j=0; j<n-i; J + +) {data[x--][y++]=count++;}}} Print two-dimensional matrix for (int i=0; i<n; i++) {for (int j=0; j<n; J + +) {COUT&LT;&LT;SETW (5) <<data[i][j]<< "";} Cout<<endl;} Destroys a two-dimensional matrix for (int i=0; i<n; i++) {delete [] data[i];} Delete [] Data;system ("pause"); return 0;}


Print Zigzag Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.