First of all, a brief idea: Spiral array because the array is arranged like a conch, so it is called the spiral array (speaking of nonsense, this is I invented) the general shape of the film is shown, then we will begin to draw circles, each lap can be regarded as four steps: First the top of the picture, the second right side draw out, the third Draw the side of the bottom and draw the fourth left side. Draw to play the first circle, then draw inside a circle, first ... Second.. Fourth.. There is no circle after the painting. Then draw the inside of the circle, until the picture does not move (beyond the maximum index of the array) and then stop; Well, that's the idea, and here's the code:
1 Static voidMain (string[] args)2 {3Console.WriteLine ("Please enter a positive integer greater than 1:");4 intn =0;5 Try6 {7 stringRead =console.readline ();8 9n =Int32.Parse (read);Ten One if(N <=1) A { -Console.WriteLine ("Please enter the correct positive integer:"); - } the } - Catch - { -Console.WriteLine ("Please enter the correct positive integer:"); + } - + A byte[,] B =New byte[n, n]; at - byteresult =1;//the output starts with a result of 1 - - intItemn = N-1;//How many lines to output - - for(intm =0; M < n/2f; m++) in { - if(Itemn >0) to { + //Output Top Line - for(inti =0; i < itemn; i++) the { *B[m, i + m] =result; $result++;Panax Notoginseng } - //Output Right Line the for(inti =0; i < itemn; i++) + { AB[i + M, Itemn + m] =result; theresult++; + } - //Output Bottom Line $ for(inti = itemn; i >0; i--) $ { -B[itemn + M, i + m] =result; -result++; the } - //Output Left lineWuyi for(inti = itemn; i >0; i--) the { -B[i + M, m] =result; Wuresult++; - } About //minus two lines $Itemn-=2; - } - Else - { AB[m, M] =result; + } the } - $ for(inti =0; I < n; i++) the { the Console.WriteLine (); the for(intj =0; J < N; J + +) the { -Console.Write (B[i, J]. ToString (). PadRight (Ten)); in } the } theConsole.read ();
View Code
The above is to see a question feel a bit of meaning and then studied the next. Because recently in learning to write e-mail, found that their writing and expression is too poor. So write down this article to exercise your writing. As an exercise, but also as a later contempt for the present evidence. If you feel childish, please spray lightly.
Simple parsing of the spiral array