注意端点值,不然有的示例不能过。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int N = s.size();
int n1 = 1, n2 = 2;
int maxn1 = -1, maxn2 = -1;
for (n2 = 3; n2 <= N; n2++) {
for (n1 = 1; n1 <= n2; n1++) {
if (n1 + n1 + n2 - 2 == N) {
if (n1 > maxn1) {
maxn1 = n1;
maxn2 = n2;
}
}
}
}
for (int i = 0; i < maxn1 -1; i++) {
cout << s[i];
for (int j = 0; j < maxn2 - 2; j++) {
cout << " ";
}
cout << s[N - i - 1] << endl;
}
for (int i = 0; i < maxn2; i++) {
cout << s[maxn1 - 1 + i];
}
cout << endl;
// cout << maxn1 << " " << maxn2 << endl;
system("pause");
return 0;
}