原文: 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
換行 輸出一個 換行符