题目
思路
一个数学问题,主要是求行数和列数,题目的要求是尽量是正方形,在n1和n3不超过n2的情况下尽可能的大
当字符串大小为偶数的时候有三种情况,为奇数的时候有三种情况,分开计算行列即可
输出用two pointers输出
代码
#include <iostream>
#include <string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<sstream>
#include<string.h>
#include<math.h>
using namespace std;
#define nmax 505
#define inf 999999
void printSpace(int num)
{
for (int i = 0; i < num; ++i){
cout << " ";
}
}
int main()
{
string str;
cin >> str;
int lie, hang;
if (str.length() % 2 != 0){
if ((str.length() + 1) % 3 == 0){
lie = (str.length() + 1) / 3;
hang = lie + 1;
}
else if ((str.length() - 1) % 3 == 0){
hang = lie = (str.length() + 2) / 3;
}
else{
lie = str.length() / 3;
hang = lie + 2;
}
}
else{
if ((str.length() + 2) % 3 == 0)
hang = lie = (str.length() + 2) / 3;
else if (str.length() % 3 == 0){
lie = str.length() / 3;
hang = lie + 2;
}
else {
lie = (str.length() + 1) / 3;
hang = lie + 1;
}
}
int i = 0, j = str.length() - 1;
while (i < lie-1){
cout << str[i];
printSpace(hang - 2);
cout << str[j]<<endl;
++i, --j;
}
while (i <= j){
cout << str[i];
++i;
}
return 0;
}