/ * Print a n*n square,N is the number of characters per side ( 3〈n〈20), requires the outermost is "X", the second layer is "Y", from the third layer each layer sequentially prints the number 0,1, 2,3,...
Example: when N = 5, print out the following graphic:
x x x x x
X y y y x
X y 0 y x
X y y y x
x x x x x
*/
1 //TESTC.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <string>//when you want to use the string class to refer to a file6#include <iostream>7#include <stdio.h>8 9 using namespaceStd//C + + engineering, this statement is necessary, or some compilers do not recognizeTen One int_tmain (intARGC, _tchar*argv[]) A { - intn= -; - intI=0, j=0, k=0; the - while(1) - { -Cin>>N; + - Char**p; +p=New Char*[n]; A at for(i=0; i<n;i++) -p[i]=New Char[n]; - - for(intk=n/2+n%2-1; k>=0; k--)//from scratch, it will be covered and must be reversed. - for(intI=0; i<n;i++) - { in for(intj=0; j<n;j++) - { to if(k==0&& (i==k| | j==k| | i==n-k-1|| j==n-k-1)) p[i][j]='x'; + Else if(k==1&& (i==k| | j==k| | i==n-k-1|| j==n-k-1)) p[i][j]='y'; - Else if(i==k| | j==k| | i==n-k-1|| j==n-k-1) p[i][j]=k-2+'0'; the } * $ }Panax Notoginseng for(intI=0; i<n;i++) - { the for(intj=0; j<n;j++) + { A theprintf"%c", P[i][j]); + } -printf"\ n"); $ } $ } - - return 0; the}
Prints a n*n square, from the outermost to the innermost one character per layer