列表

详情


NC25050. [USACO 2007 Feb L]Rows and Columns

描述

Farmer John's assistant, Jay, is not the plumpest bale of hay in the barn. When asked to arrange the cows as a rectangle in a pen and write down their serial numbers (which are unique and and in the range 1..1000) in order, he made a stupid mistake and wrote down the numbers in column order instead of row order.By way of example, for a set of 25 cows serial numbered 1..25 and arranged like this:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
He recorded 1, 16, 15, 14, 13, 2, 17, 24, 23, 12, 3, 18, ... 7, 8, This is not the way FJ's computer program needs the data to be FJ's program needs row order (1, 2, 3, 4, 5, 16, 17, ..., 10, 9).
Given both the dimensions R rows and C columns of the rectangle (1 <= R <= 20; 1 <= C <= 20) and the column order, determine the row order for FJ's data.

输入描述

Line 1: Two space-separated integers, respectively: R and C
Lines 2..R*C+1: Line i+1 lists a single integer that is the serial number of entry i in the column-ordered list Jay created.

输出描述

Lines 1..R*C: Line i contains a single integer that is entry i in the proper row-sorted list.

示例1

输入:

3 3
1
8
7
2
9
6
3
4
5

输出:

1
2
3
8
9
4
7
6
5

说明:

A 3 x 3 spiral as above:
1 2 3
8 9 4
7 6 5

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Go 解法, 执行用时: 6ms, 内存消耗: 984K, 提交时间: 2023-08-18 09:58:18

package main
import "fmt"

func main() {
    rows, cols, d := 0, 0, 0
    fmt.Scanf("%d %d\n", &rows, &cols)
    
    matrix := make([][]int, rows)
    for i := 0; i < rows; i++ {
        matrix[i] = make([]int, cols)
    }
    
    for i := 0; i < rows * cols; i++ {
        fmt.Scanln(&d)
        matrix[i % rows][i / rows] = d
    }
    for i := 0; i < rows; i++ {
        for j := 0; j < cols; j++ {
            fmt.Println(matrix[i][j])
        }
    }
}

Python3 解法, 执行用时: 43ms, 内存消耗: 4556K, 提交时间: 2023-08-18 09:47:06

rows, cols = map(int, input().split())
matrix = [['0' for i in range(cols)] for j in range(rows)]
for i in range(rows * cols):
    matrix[i % rows][i // rows] = input()
print('\n'.join('\n'.join(row) for row in matrix))

上一题