[编程题]-洗牌
【解题思路】
每次读取一个数之后,算出他经过
k
次洗牌后的位置,只用一个长度为
2n
数组用来输出 根据当前数的位置,
可以算出经过一次洗牌后的位置
如果当前数小于等于
n
(即在左手),则它下次出现的位置是
2*
当前位置
-1
如果当前位置大于
n
(即在右手)
,
则它下次出现的位置是
2*
(当前位置
- n
)
【示例代码】
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int groups = sc.nextInt();//数组组数
while (groups-- > 0){
int n = sc.nextInt();//数字个数的一半
int k = sc.nextInt();//洗牌次数
int[] res = new int[2*n];//数据总个数
for(int i=0;i<2*n;i++){ //遍历数据数组
int tmp = i + 1;//此处修正内层循环更加temp计算位置
for(int j = 0; j < k;j++){
if (tmp<= n){
tmp = 2*tmp - 1;
}else{
tmp = 2 * (tmp - n);
}
}
res[tmp - 1]=sc.nextInt();//数组中temp-1的位置的数据,即就是洗牌后的结果
}
//输出
if(res.length> 0){
System.out.print(res[0]);
}
for(int i = 1;i< 2*n;i++){
System.out.print(" "+res[i]);
}
System.out.println();
}
}
}