今日份蓝桥杯训练

时间:2022-12-10 20:55:36

题目:

今日份蓝桥杯训练

解答:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;

int main()
{
char c;//将数字和字符都考虑进去
cin >> c;
if (c >= 'A' && c <= 'Z')//字符类型,else就是数字
{
for (int row=1;row<=c-'A'+1; row++)
{
//先考虑空格
string space = string(c - 'A' + 1 - row, ' ');
cout << space;
for (int m=1;m<row;m++)
{
cout << (char)('A' + m - 1);//基准都是从A开始
}
cout << (char)('A' + row - 1);//cout打印字符加减时,需要强制类型转换
for (int m = 1; m < row; m++)
{
cout << (char)('A'+row-1 - m);//反向打印
}
cout << endl;//换行
}
}
else
{
//数字状况,直接将A变为1
for (int row = 1; row <= c - '1' + 1; row++)
{
string space = string(c - '1' + 1 - row, ' ');
cout << space;
for (int m = 1; m < row; m++)
{
cout << (char)('1' + m - 1);
}
cout << (char)('1' + row - 1);
for (int m = 1; m < row; m++)
{
cout << (char)('1' + row - 1 - m);
}
cout << endl;
}
}
return 0;
}