题目:
输入样例:
10 a
输出样例:
aaaaaaaaaa
a a
a a
a a
aaaaaaaaaa
第一次:测试点1,测试点2答案错误。
#include<bits/stdc++.h>
using namespace std;
int main()
{
char c;
int N = 0;//N,行,列
cin >> N >> c;
int row = N / 2, col = N;
for (int i = 0; i < row; ++i) {
if (i == 0 || i == row - 1)
for (int i = 1; i <= col; ++i)
cout << c;
else {
cout << c;
for (int i= 2; i <= col - 1; i++) {
cout << ' ';
}
cout << c;
}
cout << endl;
}
return 0;
}
错误分析:没有考虑四舍五入的情况。
正确代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
char c;
int N = 0;//N,行,列
cin >> N >> c;
int row = N / 2 + N % 2, col = N;
for (int i = 0; i < row; ++i) {
if (i == 0 || i == row - 1)
for (int i = 1; i <= col; ++i)
cout << c;
else {
cout << c;
for (int i = 2; i <= col - 1; i++) {
cout << ' ';
}
cout << c;
}
cout << endl;
}
return 0;
}