列表

详情


NC14305. Permutation

描述

A permutation p1, p2, ... , pn of 1, 2, ..., n is called a lucky permutation if and only if pi ≡ 0 (mod|pi - pi - 2|) for i = 3 ... n. Now you need to construct a lucky permutation with a given n.

输入描述

The first line is the number of test cases. For each test case, one single line contains a positive integer n(3 ≤ n ≤ 105).

输出描述

For each test case, output a single line with n numbers p1, p2, ... , pn. It is guaranteed that there exists at least one solution. And if there are different solutions, print any one of them.

示例1

输入:

1
6

输出:

1 3 2 6 4 5

原站题解

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

C++14(g++5.4) 解法, 执行用时: 35ms, 内存消耗: 1656K, 提交时间: 2020-04-14 23:07:57

#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
	int T,n;
	cin >>T;
	while(T--){
		cin >> n;
		 for(int i =1;i <=n;i++){
		 	if(i%2 == 0)cout << (n+1)-(i+1)/2 << " ";
		 	else cout << (i+1)/2 << " ";
		 }
		 cout << endl;
	}
	return 0;
}

C++(clang++11) 解法, 执行用时: 19ms, 内存消耗: 1596K, 提交时间: 2021-03-09 20:47:41

#include<iostream>
using namespace std;
int main(){
	int t,n;
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1;i<=n;i++){
			if(i%2==0){
				printf("%d ",i/2+(n+1)/2);
			}
			else{
				printf("%d ",(i+1)/2);
			}
		}
	}
}

Python3 解法, 执行用时: 1015ms, 内存消耗: 5816K, 提交时间: 2022-10-22 03:36:57

for _ in range(int(input())):
    n = int(input())
    for i in range(0, n, 2):
        print(i // 2 + 1, end = ' ')
        if(i < n - 1):
            print((n + 1) // 2 + i // 2 + 1, end = ' ')
    print()

上一题