UVA 769 - Magic of David Copperfield(构造问题)

 Magic of David Copperfield 

The well-known magician David Copperfield loves to show the following trick: a square with N rows and Ncolumns of different pictures appears on a TV screen. Let us number all the pictures in the following order:

12$\dots$N
$\vdots$$\vdots$$\ddots$$\vdots$
N*(N-1)+1N*(N-1)+2$\dots$N*N

Each member of the audience is asked to put a finger on the upper left picture (i.e., picture number one) and The Magic begins: the magician tells the audience to move the finger k1 times through the pictures (each move is a shift of the finger to the adjacent picture up, down, left or right provided that there is a picture to move to), then with a slight movement of his hand he removes some of the pictures with an exclamation ``You are not there!", and ... it is true - your finger is not pointing to any of the pictures removed. Then again, he tells the audience to make k2 moves, and so on. At the end he removes all the pictures but one and smiling triumphantly declares, ``I've caught you" (applause).


Just now, David is trying to repeat this trick. Unfortunately, he had a hard day before, and you know how hard to conjure with a headache. You have to write a program that will help David to make his trick.

Input 

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

Each test case consists of a single integer number N ( $2 \le N \le 100$).

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Your program should write the following lines with numbers to the output file:


$k_1 \ x_{1,1} \ x_{1,2} \ \dots \ x_{1,m1}$

$k_2 \ x_{2,1} \ x_{2,2} \ \dots \ x_{2,m2}$

$\dots$

$k_e \ x_{e,1} \ x_{e,2} \ \dots \ x_{e,me}$


where ki is a number of moves the audience should make on the i-th turn ( $2N \le k \le 10000$). All ki should be different (i.e. ki <> kj when i <> j). $x_{i,1}, x_{i,2}, \dots, x_{i,mi}$ are the numbers of the pictures David should remove after the audience will make ki moves (the number of the pictures removed is arbitrary, but each picture should be listed only once, and at least one picture should be removed on each turn).


A description of the every next turn should begin with a new line. All numbers on each line should be separated by one or more spaces. After e iterations, all pictures except one should be removed.

Sample input 

1

3

Sample Output 

8     4 6
13    9
10    7 1
7     8
11    3 5

题意:大卫科波菲尔表演魔术,让一个个观众上台,手指从第一张卡片开始移动一个步数,然后大卫删除掉一些观众不可能最终指到的卡片,直到最后删除到剩1张。

思路:想通了其实很简单的构造问题,以斜对角线为行号,如果走的奇数步,那么必然不会停留在当前行号,这样每次走奇数步,然后把当前一行消掉,最终走到右下角就是答案。

代码:

#include <stdio.h>
#include <string.h>

int t, n, k, m;

void init() {
	scanf("%d", &n);
	k = 2 * n + 1;
	m = 2;
}

void solve() {
	while (k && m < 2 * n) {
		printf("%d", k);
		for (int i = 1; i < m; i++) {
			if (i > n || m - i > n) continue;
			printf(" %d", (i - 1) * n + (m - i));
		}
		printf("\n");
		m++; k += 2;
	}
	if (t) printf("\n");
}

int main() {
	scanf("%d", &t);
	while (t--) {
		init();
		solve();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值