HDU 6235 java描述容易

       HDU 6235  java描述

Problem

A permutation p1,p2,...,pnp1,p2,...,pn of 1,2,...,n1,2,...,n is called a lucky permutation if and only if pi≡0(mod|pi−pi−2|)pi≡0(mod|pi−pi−2|) for i=3...ni=3...n. 

Now you need to construct a lucky permutation with a given nn. 

Input

The first line is the number of test cases. 

For each test case, one single line contains a positive integer n(3≤n≤105)n(3≤n≤105). 

Output

For each test case, output a single line with nn numbers p1,p2,...,pnp1,p2,...,pn. 

It is guaranteed that there exists at least one solution. And if there are different solutions, print any one of them. 

Sample Input

1
6

Sample Output

1 3 2 6 4 5

这道题的题意是这样的,输入一个数表示测试的数据,然后再输入一个数字,比如6 ,那就代表有一个数组 1 2 3 4 5 6.  然后给数组排序,排序的规则满足pi≡0(mod|pi−pi−2|),也就是说,排好顺序的数组间隔差一,这里分建立两个数组,比如一个 1 2 3 4  , 一个 4 5 6,然后把 4 5 6,插入 1 2 3 4当中构成 1 4 2 5 3 6 4 ,也可以满足题意。

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int r = sc.nextInt();
			for (int y = 0; y < r; y++) {
				int scr = sc.nextInt();
				if (scr == 1) {
					System.out.println(1);
				} else {
					long arr[] = new long[scr + 1];
					long a[] = new long[arr.length / 2];
					long b[] = new long[arr.length / 2];
					int tom = 1;
					for (int i = 0; i < a.length; i++) {
						a[i] = tom;
						tom++;
					}
					for (int i = 0; i < a.length; i++) {
						b[i] = tom;
						if (tom == scr) {
							break;
						}
						tom++;
					}
					int sum = 0;
					for (int i = 0; i < a.length; i++) {
						
						if (a[i] != 0 && sum != 0) {
							System.out.print(" "+a[i]);

							sum++;
						}
						if (a[i] != 0 && sum == 0) {
							System.out.print(a[i]);

							sum++;
						}
					
						if (b[i] != 0 &&sum != 0) {
							System.out.print(" "+b[i]);
							sum++;
						}
					}

					System.out.println();

				}

			}
		}
	}
}

有的地方有写没有,也不想改了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值