You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
/*
测试样例:
abcdefgh
输出:
aebfcgdh
思考:
行数n, 列数m 需要满足 m>=n
(1) 1行 m=length(abcdefgh)=8 n=1 满足m>=n
(2) 2行 m=8/2=4 n=2 满足m>=n
(3) 3行 8不能被3整除跳过尝试继续增大行数试试
(4) 4行 m=8/4=2, n=4 m<n 不满足条件了break
*/
const int N = 210;
char a[N][N]; // 开一个二维数组用来模拟
int n, m; // 最后是几行几列
int main() {
string s;
cin >> s; // 生成一个n行m列的矩阵
// 1、几行几列是答案
int len = s.size();
for (int i = 1;; i++) {
if (len % i == 0 && len / i >= i) n = i; // 找到一个合理解,更新之
if (len / i < i) break; // 跑过了,就停下来
}
m = len / n; // 每行多少列
// 2、做一个二维矩阵存进去
for (int i = 0; i < n; i++) // 行
for (int j = 0; j < m; j++) // 列
a[i][j] = s[i * m + j];
// 3、从第一列开始逐列从上下到输出矩阵中的字符
for (int j = 0; j < m; j++)
for (int i = 0; i < n; i++)
cout << a[i][j];
return 0;
}