C language: A Example of print snake Matrix

Source: Internet
Author: User

 

 // Snail kematrix. cpp: defines the entry point for the console application.  // Create by Xianyi. Ye, May 4,2010 # Include" Stdafx. h "# Include <iostream. h> /* Question: print a Sanke matrix as followingi/j1234561 12910252624381124273567122328416151413222851718192021306363534333231 explain: "I" is the line number of the Matrix. "J" is the column number of the matrix .*/  Void Showmatrix ( Int N ){ For ( Int I = 1; I <= N; I ++ ){ For ( Int J = 1; j <= N; j ++ ){ If (I <= J) // Upper triangular matrix { If (J % 2 = 0) // J = even number Cout <(J-1) * (J-1) + I <"  "; Else  // J = odd number Cout <(J-1) * (J-1) + 2 * j-I <"  ";} If (I> J) // Lower triangular matrix { If (I % 2 = 0) // I = even number Cout <(I-1) * (I-1) + 2 * I-j <"  "; Else  // I = odd number Cout <(I-1) * (I-1) + j <"  ";}} Cout <// create new line .}} Int Main ( Int Argc, Char * Argv []) {showmatrix (6 ); Return 0 ;}

 

 

Result:

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: info-contact@alibabacloud.com 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.