NC14305. Permutation
描述
输入描述
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()