1. 程式人生 > >【學堂線上】列印空心數字菱形

【學堂線上】列印空心數字菱形

輸入描述
輸入一個大寫字母與一個整數n,1 <= n <= 26

輸出描述
輸出如下高度為2n - 1的菱形;

注意:A的下一個字母是B,Z的下一個字母是A

樣例輸入
R 12

樣例輸出
在這裡插入圖片描述

#include<iostream>
using namespace std;

int main(){
	char m;
	int n;
	cin >> m >> n;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= (2*n-1-(2*i-1))/2; j++
) cout << " "; cout << m ; for (int j = 1; j <= 2 * (i - 1) - 1; j++) cout << " "; while (i != 1){ cout << m; break; } if (m == 'z') m = 'a'; else if (m == 'Z') m = 'A'; else m++; for (int j = 1; j <= (2 * n - 1 - (2 * i - 1)) / 2; j++) cout <<
" "; cout << endl; } for (int i = 0; i < 2; i++){//將m減減兩次 if (m == 'a') m = 'z'; else if (m == 'A') m = 'Z'; else m--; } for (int i = 1; i <= n - 1; i++){ for (int j = 1; j <= i; j++) cout << " "; cout << m; for (int j=2*(n-1-i)-1; j>=1; j--)
cout << " "; while (i != n - 1){ cout << m; break; } if (m == 'a') m = 'z'; else if (m == 'A') m = 'Z'; else m--; for (int j = 1; j <= i; j++) cout << " "; cout << endl; } return 0; }