"title" Square Fill number: In a n*n square, fill in ... The number of n*n, and requires the following form:
Fig. 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 "Get Started" observe the legend, it is not difficult to see this is a spiral matrix, the following is a large version of the color gradient, pure hand-made (with Wood has 2048 bright): "Core Code"
1//variable Assignment initial value2x:=0;//Horizontal Axis3y:=0;//Ordinate4i:=0;//number to fill in5k:=n;//the number of squares to fill each round6j:=1;//The direction of the number of fills,1From top to bottom ↓ or left to right →,-1when the opposite7//start to fill the number8 whileI<n*n Do9 beginTen fori:=i+1 toI+k Do//Vertical ↑↓ Fill number Onex:x+J; Aa[x,y]:=i; - End;//End forLoops -Dec (k);//IE k:=k-1, the number of squares to be filled in the next line after each completion of a vertical line of fill-1 the ifj=1 ThenJ:=-1Elsej:=1;//control direction, this time from top to bottom or from left to right next time from bottom to top or right to left - fori:=i+1 toI+k Do//horizontal → fill number - begin -y:=y+J; +a[x,y]:=i; - End; + End;//End whileCycle
Two-dimensional arrays in Pascal language: Matrix problems