输出如下图形:
A A A A A A A A A A A A A
A B B B B B B B B B B B A
A B C C C C C C C C C B A
A B C D D D D D D D C B A
A B C D E E E E E D C B A
A B C D E F F F E D C B A
A B C D E F G F E D C B A
A B C D E F F F E D C B A
A B C D E E E E E D C B A
A B C D D D D D D D C B A
A B C C C C C C C C C B A
A B B B B B B B B B B B A
A A A A A A A A A A A A A
我找到了两种不同形式的思路,一种是以输出行数为标准,一种是以圈数为标准,各有优点,而且对编辑类似图形有启发.
第一种,以输出行数为标准,用户输入想输出的行数,利用该位置是否已被占和是否为边界位置进行双重判定,满足条件则将该位置存入数组,最后输出数组.源代码如下:
#include<stdio.h>
#include<stdlib.h>
int main (void)
{
FILE *fp;
int i, sum, total, row, line;/*sum表示总的圈数*/
char ch;
char count[80][80];
puts("Enter the total of row: ");
scanf("%d",&total);
for(row=1; row<=total; row++)
for(line=1; line<=total; line++)
count[row][line]=0 ;
if(total%2 == 0)/*根据输入数据的奇偶性,确定圈数*/
sum=total/2;
else
sum=total/2+1;
for(ch=65,i=1; i<=sum; i++,ch++)/*sum表示总的圈数*/
for(row=1; row<=total; row++)
for(line=1; line<=total; line++)
if (0 == count[row][line])/*该位置是否已被占*/
if( row == i || row ==(total-i+1) || line == i || line == (total-i+1) )/*是否为边界位置*/
count[row][line]=ch;
if ( (fp=fopen("sdas.txt","w")) == NULL)
{
fprintf(stderr,"nError opening file n");
exit(1);
}
for(row=1; row<=total; row++)/*输出数组(即图案)*/
{
for(line=1; line<=total; line++)
{
fprintf(fp, "%c ", count[row][line]);
fprintf(stdout, "%c ", count[row][line]);
}
fprintf(fp, "n");
fprintf(stdout, "n");
}
system("pause");
return 0;
}
第二种,以输出圈数为标准,用户输入想输出的圈数,利用循环语句分别输出上下左右四条边,注重每个圈往里缩一个位置.源代码如下:(注:头文件和输出语句相同)
int main (void)
{
FILE *fp;
int i, total, row, line;
char ch;
char count[80][80];
puts("Enter the total of rings: ");
scanf("%d",&total);
ch=65;
for( i=1; i<=total; i++)/*利用循环语句分别输出上下左右四条边,注重每个圈往里缩一个位置*/
{
for(row=i, line=i; line<=2*total-i; line++)
count[row][line]=ch;
for(row=2*total-i, line=i; line<=2*total-i; line++)
count[row][line]=ch;
for(row=i, line=i; row<=2*total-i; row++)
count[row][line]=ch;
for(line=2*total-i, row=i; row<=2*total-i; row++)
count[row][line]=ch;
ch++;
}
第三种,综合前两种的优点,综合利用循环语句和判定语句输出上下左右四条边,注重每个圈往里缩一个位置.
源代码如下:(注:多数和第二种相同,只输出改编部分)
for( i=1; i<=total; i++)/*利用判定语句输出上下左右四条边,注重每个圈往里缩一个位置*/
{
for(row=i; row<=2*total-i; row++)
for(line=i; line<=2*total-i; line++)
if(row == i || row == 2*total-i || line == i || line == 2*total-i)
count[row][line]=ch;
ch++;
}
呵呵,一点心得,和各位网友分享,假如你能从中得到一点启发,将是我莫大的骄傲!