7-4 Programming and printing hollow character diamonds (10 points)

7-4 Programming and printing hollow character diamonds (10 points)

This question requires reading the starting letter of the diamond and the height of the diamond, and then output the hollow character diamond. The so-called "hollow diamond" means: each line is composed of character strings with letters at both ends and spaces in the middle, and the center of each line is aligned; the upper half of the two adjacent lines have a difference of 2 in length, and the letters are given The initial letters of is increasing one by one; the lower part is symmetrical to the upper part.

Input format:
Enter the starting letter (the range is English capital letters AG) and the height of the diamond (an odd number not exceeding 10) in one line.

Output format:
output hollow character diamond.

Input sample:

B 5
output example:
Insert picture description here

#include <stdio.h>
// 1、先画出实心菱形(把菱形分成上下俩部分,再分成正三角和倒三角 ) 
// 2、 把输入的N(n层)带入循环 
// 3、 用if语句使菱形中心为空
// 4、 利用ASCII码的加减  实现字母组成 
int main()
{
    
    
	int n,i,j;
	char c;
	scanf("%c%d",&c,&n);
	//针对奇数层的菱形 上部分
	for(i=1;i<=n/2+1;i++)
	{
    
    
		//第一个为上班边的倒三角 
		for(j=n/2;j>=i;j--)
			printf(" ");
		//第二个上半边的正三角 
		for(j=0;j<i*2-1;j++)
		{
    
    
			//菱形中间的空心用if else来实现 
			if(j==0 || j==i*2-2) 
				printf("%c",c);
			else 
				printf(" ");
		}
		c+=1;
		printf("\n");
	}
	c-=1; 
	//下部分
	for(i=1;i<=n/2;i++)
	{
    
    
		c-=1;
		//第三个为下半边的正三角 
		for(j=1;j<=i;j++)
			printf(" ");
		//第四个三角形为倒三角形
		for(j=n-2;j>=i*2-1;j--)
		{
    
    
			if(j==n-2 || j==i*2-1) 
				printf("%c",c);
			else 
				printf(" ");
		} 
		printf("\n");
	}
}

Guess you like

Origin blog.csdn.net/xiahuayong/article/details/109191143