原文: http://acm.timus.ru/problem.aspx?space=1&num=1506
背景: 对输入的一列数组进行格式化输出, 指定k列, 要求:
Output the N numbers
given in the input in K columns in such a way that the number
of lines is minimal and the columns have the same height with the possible exception of the last column, which may be shorter
除最后一列(行数可以少点) 其余列拥有相同的行. 具体Sample可见下:
Sample
input | output |
---|---|
7 3 1 2 30 40 50 600 700 |
1 40 700 2 50 30 600 |
7 3 -- n =7 , k = 3. 3列 最多三行 n/k = 2 set line = (n+k-1) / k 可表示最大行数.
显然 k是不会超过n的. 考虑Sample中输出的各元素 位置解析, 按行分析.
1 40 700 分别在原来数组中下标为: 0, 3 (0+k), 6(0+2k)
2 50 分别在原来数组中下标为: 1, 4(1+k) (1+2k)不存在, 不输出
30 600 类似
因此, 大致思路就明了, 二重循环
for i : 0 --> line (最大行数)
将 mod k 相同的元素 从小到大输出 步长为k
换行 输出一个 换行符