题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1506
Every New Russian has to look through long columns of numbers for analyzing market trends and planning his investments. Psychologists assure that the longer is a column of numbers, the more difficult
it is to perceive it. Therefore, it is better to print numbers not in one long column, but in several columns so that their height would be minimal. Transform a given sequence of numbers to a format that is psychologically more convenient for perception.
Input
The first line contains two integers: N (1 ≤ N ≤ 100), which shows how many numbers must be analyzed, and K (1 ≤ K ≤ N), which is the desired number of columns. The second
line contains Ninteger numbers in the range from 0 to 999.
Output
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. The width of each column must be 4 symbols; the numbers must be aligned to the right edge and padded with spaces to the required width. The numbers must be given in the same order as in the input, but in columns: the first column
from the top to the bottom, then the second column from the top to the bottom, and so on. All nonempty lines must end with a line break; there must be no end spaces in the lines. It is guaranteed that solution is always exist.
Sample
input | output |
---|---|
7 3 1 2 30 40 50 600 700 |
1 40 700 2 50 30 600 |
PS:
每列按着顺序跑一遍就好了!
代码如下:
#include <cstdio> #include <cmath> int main() { int n, k; int a[147], b[147][147]; while(~scanf("%d%d",&n,&k)) { for(int i = 0; i < n; i++) { scanf("%d",&a[i]); } int c_num = (ceil)(n/(k*1.0)); int ss = c_num*k-n; int lc = 0, lr = 0; for(int i = 0; i < n; i++) { b[lr][lc] = a[i]; lr++; if(lr >= c_num) { lc++; lr = 0; } } int num = 0; int flag = 0; for(int i = 0; i < c_num; i++) { for(int j = 0; j < k; j++) { if(j == k-1 && i >= c_num-ss) { flag = 1; printf("\n"); break; } printf("%4d",b[i][j]); flag = 0; } if(!flag) printf("\n"); } printf("\n"); } return 0; }