蓝桥杯-基础练习-字母图形
动态分配空间实现,两个测试过不去,80分
#include <iostream>
using namespace std;
int main(){
int n,m;
cin >> n >> m;
char **a = new char*[n];
for(int i = 0; i < n; i++)
a[i] = new char[m];
for(int i = 0; i < n; i++){
int k = 0;
for(int j = i; j < m; j++){
a[i][j] = 'A'+ k;
k++;
}
k = 0;
for(int j = i; j >=0;j--){
a[i][j] = 'A'+k;
k++;
}
}
for(int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cout << a[i][j];
}
cout << endl;
delete []a[i];
}
delete []a;
return 0;
}
数组固定分配内存,测试全过。
#include <iostream>
using namespace std;
int main(){
int n,m;
cin >> n >> m;
char a[26][26] = {0};
for(int i = 0; i < n; i++){
int k = 0;
for(int j = i; j < m; j++){
a[i][j] = 'A'+ k;
k++;
}
k = 0;
for(int j = i; j >=0;j--){
a[i][j] = 'A'+k;
k++;
}
}
for(int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cout << a[i][j];
}
cout << endl;
}
return 0;
}