URAL 1506. Columns of Numbers(模拟啊 )

题目链接: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;
}

时间: 2024-10-06 16:33:34

URAL 1506. Columns of Numbers(模拟啊 )的相关文章

USACO Runaround Numbers 模拟

根据题意的 Runaround 规则去找比当前数大的最近的一个 Runaround数字 模拟题~ Source code: /* ID: wushuai2 PROG: runround LANG: C++ */ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream>

URAL 1290. Sabotage(STL &amp; 模拟啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1290 1290. Sabotage Time limit: 1.0 second Memory limit: 64 MB It is the seventh year of the terrible harmful Galaxy War... Leo Hao is one of the first defenders of his planet. He is lucky! He has gon

UVA 12050 - Palindrome Numbers 模拟

题目大意:给出i,输出第i个镜像数,不能有前导0. 题解:从外层开始模拟 #include <stdio.h> int p(int x) { int sum, i; for(sum=i=1;i<=x;i++) sum *= 10; return sum; } int main() { int n, i, j, t, cs[1000], c; while(~scanf("%d", &n)) { if(n==0) break; i=1; while(n>9*

URAL(timus) 1280 Topological Sorting(模拟)

Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world championship in programming and decided to study N subjects (for convenience we will number these subjects from 1 to N). Michael has worked out a study plan

LeetCode 2 Add Two Numbers 模拟,读题 难度:0

https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked

URAL 1712. Cipher Grille (模拟)

1712. Cipher Grille Time limit: 0.5 second Memory limit: 64 MB Our program committee uses different tools for problem development: a mailing list, a version control system, an administration system of the Timus Online Judge website, and many others.

Using Linux

Part 1: Graphical vs. Non-Graphical Mode Linux can be used in one of two ways: graphically and non-graphically. In graphical mode your applications live in windows that you can resize and move around. You have menus and tools to help you find what yo

位运算 格雷码 gray

题目: It is necessary to arrange numbers from 0 to 2^(N+M)-1 in the matrix with 2^N rows and 2^M columns. Moreover, numbers occupying two adjacent cells must differ only in single bit in binary notation. Cells are adjacent if they have common side. Mat

二维数组做函数参数、指向指针的指针做函数参数

这里有一篇文章  写的很好http://blog.csdn.net/f81892461/article/details/8974087 该文章中有一句话  总之就是:数组无法作为参数,总会被编译器将地址赋值给形参指针的,即使指针定义成数组形式,也还是指针.然后各种差别都是由数组和指针不同的定位元素的方式导致的. 这句话说的很对啊,数组做形参的是时候都是被当成指针来处理的.不明白这句话的可以看一下,参考文章链接里的那幅图,注意比较一下下图中的两个:data+1,第一个data+1指向的是元素dat