思路及代码
去尾加头
认为示例给的不好
比如输入 6 3
输出是 ABC BAB CBA ABC BAB CBA
还是 ABC BAB CBA DCB EDC FDE呢
是后者
//input n,m int 1<= <=26
#include<iostream>
#include<vector>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
//solution
//initial alp
char alp[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
//initial vector ABCD...
vector<char> list;
for (int i = 0; i <= m-1; i++){
list.push_back(alp[i]);
}
//row 1
for (char ele: list){
cout << ele;
}
cout << endl;
//other rows :遍历 1->n-1 去尾加头
for (int j = 1; j <= n-1; j++){
list.pop_back();
list.insert(list.begin(), alp[j]);
for (char ele: list){
cout << ele;
}
cout << endl;
}
return 0;
}
参考:1️⃣ C++数组(数组定义方式、数组名作用、数组地址)_c++定义数组-CSDN博客
收获:1️⃣复习 vector 定义,在开头添加元素 .insert(...,...),删除最后一个元素.pop_back()
菜菜,不是教程,做题和学习记录
标签:...,CBA,20,OJ,int,字母,vector,20%,DHU From: https://blog.csdn.net/jtymyxmz/article/details/141102457