武汉科技大学ACM :1001: 华科版C语言程序设计教程(第二版)课后习题3.12

Problem Description

输入n,输出对应的边长为n的空心正六边形。

为方便看图,样例中点 '.' 表示空格,打印图形时请打印空格而非小圆点。

Input

 边长n.(n<=20)

Output

 边长为n的正六边形

 

Sample Input

5

Sample Output

.....*****
....*.....*
...*.......*
..*.........*
.*...........*
..*.........*
...*.......*
....*.....*
.....*****
 1 #include<stdio.h>
 2 #include<math.h>
 3 void prt(char c, int count)  
 4 {  
 5     while(count--)  
 6     {  
 7         printf("%c",c);  
 8     }  
 9 }  
10 
11 int main()  
12 {  
13     int N, L;
14     while(scanf("%d",&N)!=EOF)
15     {
16         if(N>0 && N<=20)
17         {
18             L=0;
19             for(; L < 2 * N - 1; L++)  
20             {  
21                 prt(' ', 1); 
22                 prt(' ', abs(N - L-1)); 
23                 
24                 if(0 == L || L == 2 * N - 2)  
25                 {  
26                     prt('*', N);  
27                 }  
28                 else  
29                 {  
30                     prt('*', 1);
31                     prt(' ', 3 * N - 4 -(abs(L+1 - N) * 2));
32                     prt('*', 1);
33                 }  
34                 printf("\n");
35             }            
36         }    
37     }    
38     return 0;  
39 }  

 

posted @ 2014-12-12 13:26  liuwt365  阅读(539)  评论(0编辑  收藏  举报