// 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: