Prints a n*n square, from the outermost to the innermost one character per layer

Source: Internet
Author: User

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

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.