第一行一个数T(T ≤ 100),表示数据组数。对于每组数据,第一行两个数n,k(1 ≤ n,k ≤ 100),接下来有2n行个数a1,a2,...,a2n(1 ≤ ai ≤ 1000000000)。表示原始牌组从上到下的序列。
对于每组数据,输出一行,最终的序列。数字之间用空格隔开,不要在行末输出多余的空格。
3 3 1 1 2 3 4 5 6 3 2 1 2 3 4 5 6 2 2 1 1 1 1
1 4 2 5 3 6 1 5 4 3 2 6 1 1 1 1
import java.util.Scanner; /** * 每次读取一个数之后,算出他经过k次洗牌后的位置,只用一个长度为2n数组用来输出 * 根据当前数的位置,可以算出经过一次洗牌后的位置 * 如果当前数小于等于n(即在左手),则他下次出现的位置是 2*当前位置-1 * 如果当前位置大于n(即在右手),则他下次出现的位置是 2*(当前位置 - n) * 个人建议在线面试题的时候如果5分钟内没想到好方法建议就使用暴力方法, * 毕竟测试用例不通过什么都没用 * Created by lizo on 2016/8/20. */ 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; for(int j = 0; j < k;j++){ if (tmp <= n) tmp = 2*tmp - 1; else tmp = 2 * (tmp - n); } res[tmp - 1]=sc.nextInt(); } //输出 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(); } } }
#include<iostream> #include<vector> using namespace std; int main() { int T, n, k; cin >> T; while (T--) { cin >> n >> k; int num = 2 * n; vector<int> table(num); for(int i = 0; i < num; ++i) cin >> table[i]; while (k--) { vector<int> n1(table.begin(), table.end()); for (int i = 0; i < n; ++i) { table[2 * i] = n1[i]; table[2 * i + 1] = n1[i + n]; } } for(int i = 0; i < num - 1; ++i) cout << table[i] << " "; cout << table[num - 1] << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; class Shuffling{ vector<int> cards,left,right; int cardsNum; // 切牌 void split(int n, vector<int>& cards) { for(int i=0; i<n; i++) { left.assign(cards.begin(), cards.begin()+n); } for(int j=n; j<2*n; j++) { right.assign(cards.begin()+n, cards.end()); } } // 洗牌 vector<int> insert(int n, vector<int>& cards) { split(n, cards); vector<int> newCards; for(int i=0; i<n; i++) { newCards.push_back(left[i]); newCards.push_back(right[i]); } return newCards; } public: Shuffling(int n):cardsNum(n){}; void shufflingCards(int n, int k, vector<int>& cards) { for(int i=0; i<k; i++) { cards = insert(n, cards); } // 对于每组数据,输出一行,最终的序列。 // 数字之间用空格隔开,不要在行末输出多余的空格。 // 因为读题不仔细,最后这个“多余的”空格,排查了好长时间啊!!! for(int i=0; i<(2*n-1); i++) { cout << cards[i] << " "; } cout << cards[2*n-1] << endl; } }; int main(){ int T; cin >> T; while(T--) { int n,k; cin >> n >> k; vector<int> cards; for(int i=0; i<2*n; i++) { int temp; cin >> temp; cards.push_back(temp); } Shuffling S(n); S.shufflingCards(n,k,cards); } return 0; }
直接暴力也能过嘛 把下标暴力一下即可
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 100 + 5;
int a[2*maxn], T, n, k, ind[2*maxn], tmp[2*maxn];
void shuffle(){
int k = 1;
for(int i = 1; i <= n; i++){tmp[k] = ind[i]; k += 2;}
k = 2;
for(int i = n + 1; i <= 2*n; i++){tmp[k] = ind[i]; k += 2;}
memcpy(ind, tmp, sizeof(tmp));
}
int main(){
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &k);
for(int i = 1; i <= n*2; i++) {scanf("%d", &a[i]);ind[i] = i;}
while(k--) {shuffle();}
for(int i = 1; i < n*2; i++) printf("%d ", a[ind[i]]);
printf("%d\n", a[ind[2*n]]);
}
}
package niuke.wangyi; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; /** * @author NikoBelic * @create 16/8/30 23:38 */ public class FlushCard { public List<Integer> flush(List<Integer> cards,int n) { List<Integer> leftCards = cards.subList(0,n); List<Integer> rightCards = cards.subList(n,2*n); List<Integer> result = new ArrayList<>(); int rightIndex,leftIndex; for (int i = 0; i < n; i++) { result.add(rightCards.get(n-i-1)); result.add(leftCards.get(n-i-1)); } for (int i = 0; i < n; i++) { int temp = result.get(i); result.set(i,result.get(2 * n - i - 1)); result.set(2*n-i-1,temp); } return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int count = scanner.nextInt(); for (int i = 0; i < count; i++) { int n = scanner.nextInt(); int k = scanner.nextInt(); List<Integer> result = new ArrayList<>(); for (int j = 0; j < 2*n; j++) { result.add(scanner.nextInt()); } for (int i1 = 0; i1 < k; i1++) { result = new FlushCard().flush(result,n); } for (int m = 0; m < result.size(); m++) { System.out.print(result.get(m)); if (m != result.size() - 1) System.out.print(" "); } System.out.println(); } } }
#include <iostream> #include <vector> using namespace std; // 一个循环就行了 void shuffle(unsigned int n, int k, std::vector<unsigned int>& out) { std::vector<unsigned int> tmp; while (k--) { tmp = out; for (unsigned int i = 0; i < n; ++i) { out[2 * i] = tmp[i]; out[2 * i + 1] = tmp[n + i]; } } } int main() { int T, k; unsigned int n; cin >> T; while(T--) { cin >> n >> k; std::vector<unsigned int> out(2 * n); for (unsigned int j = 0; j < 2 * n; ++j) { cin >> out[j]; } shuffle(n, k, out); for (unsigned int j = 0; j < 2 * n; ++j) { if (j == 0) { cout << out[j]; } else { cout << ' ' << out[j]; } } cout << endl; } return 0; }
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);// 使用Scanner来获取键盘输入 // 第一次输入的是T // T:要洗牌的牌组数量 int t = scanner.nextInt(); for (int i = 0; i < t; i++) { // 第二次输入的是n和k // n:2n即牌的数量 // k:洗牌的次数 int n = scanner.nextInt(); int k = scanner.nextInt(); // 获得牌组cards,并分成两堆cards1和cards2 ArrayList<Integer> cards = new ArrayList<>(); ArrayList<Integer> cards1 = new ArrayList<>(); ArrayList<Integer> cards2 = new ArrayList<>(); // 获得牌组cards所有数 for (int j = 0; j < 2 * n; j++) { cards.add(scanner.nextInt()); } Collections.reverse(cards);// 将牌组顺序翻转,便于洗牌 for (int x = 0; x < k; x++) { cards1.clear(); cards2.clear(); //将下半堆放入cards1 for (int j = 0; j < n; j++) { cards1.add(cards.get(j)); } // 将上半堆放入cards2 for (int j = n; j < 2 * n; j++) { cards2.add(cards.get(j)); } // 洗牌过程 cards.clear(); for (int j = 0; j < n; j++) { cards.add(cards1.get(j)); cards.add(cards2.get(j)); } } Collections.reverse(cards);// 再把牌组顺序反转为正常顺序 System.out.println( cards.toString() .replace("[", "") .replace("]", "") .replace(",", "")); } scanner.close(); } }
#include "iostream" #define MAX 201 typedef long long LL; using namespace std; int n, k; LL a[MAX], b[MAX]; void solve(LL *a) { for (int i = 1; i <= 2 * n; i++) { if (i <= n) { b[2 * i - 1] = a[i]; } else b[2 * (i - n)] = a[i]; } for (int i = 1; i <= 2 * n; i++) { a[i] = b[i]; } } int main() { int t; cin >> t; while (t--) { cin >> n >> k; for (int i = 1; i <= 2 * n; i++) cin >> a[i]; while (k) { solve(a); k--; } for (int i = 1; i < 2 * n; i++) { cout << a[i] << " "; } cout << a[2 * n]; cout << endl; } }2个数组,模拟下然后过,我这个版本0.0思路也很简单。
/*这个洗牌其实就是把好比123456分成两份,123和456,然后先放把123放进 大小为6的vec中:变成1_2_3_,再把456反向放入,变成142536,这就是一次 洗牌的过程。 */ #include <iostream> #include <vector> using namespace std; vector<int> shuffle(vector<int> v) { vector<int> ans(v.size()); int k = 0; for(int i = 0; i < v.size() / 2; i ++) { ans[k] = v[i]; k += 2; } k = v.size() - 1; for(int i = v.size() - 1; i >= v.size() / 2; i --) { ans[k] = v[i]; k -= 2; } return ans; } int main() { int T; cin >> T; while(T --) { int n, k; cin >> n >> k; vector<int> poke(2 * n); for(int i = 0; i < 2 * n; i ++) { cin >> poke[i]; } while(k --) { poke = shuffle(poke); } cout << poke[0]; for(int i = 1; i < poke.size(); i ++) { cout << " " << poke[i]; } cout << endl; } }
#include <cstdio> #include <cstring> int a[1000][2]; int main() { int T, n, k; scanf("%d", &T); while (T--) { scanf("%d %d", &n, &k); for (int i = 0; i<2 * n; i++) scanf("%d", &a[i][0]); int index = 1; while (k--) { for (int i = 0; i<n; i++) { a[2 * i][index] = a[i][1 - index]; a[2 * i + 1][index] = a[i + n][1 - index]; } index = 1 - index; } for (int i = 0; i<2 * n - 1; i++) printf("%d ", a[i][1 - index]); printf("%d\n", a[2 * n - 1][1-index]); } return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> getSqe(int n,int times){ vector<int>all(2*n); vector<int>up(n); vector<int>down(n); vector<int>tmp(2 * n); for (int i = 0; i < 2 * n; i++){ cin >> all[i]; } while (times--){ for (size_t i = 0; i < n; i++) { up[i] = all[i]; down[i] = all[n + i]; tmp[2 * i] = up[i]; tmp[2 * i + 1] = down[i]; } all = tmp; } return tmp; } int main(){ int nGroups; cin >> nGroups; int times,n; vector<vector<int>>result; while (nGroups--) { cin >> n>>times; result.push_back(getSqe(n, times)); } for (auto it = result.begin(); it != result.end(); it++){ int i = 0; for (auto jt = (*it).begin(); jt != (*it).end(); jt++){ if (++i == (*it).size()){ cout << *jt; } else{ cout << *jt << " "; } } cout << endl; } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.Stack; public class Main { static private Main ss=new Main(); private Stack<Integer> left=new Stack<Integer>();//表示左手边的牌 private Stack<Integer> right=new Stack<Integer>();//表示右手边的牌 public void pokeSort(List list,List list2,List list3){//list3表示总的牌数 list.clear(); list2.clear(); for(int i=0;i<list3.size()/2;i++) list.add(list3.get(i));//将前n张牌给左手边 for(int i=list3.size()/2;i<list3.size();i++) list2.add(list3.get(i));//将后n张牌给右手边 left.addAll(list);//分别入栈 right.addAll(list2); list3.clear(); while(!left.empty()){//模拟一次洗牌过程,将结果放入list3中 list3.add(right.pop()); list3.add(left.pop()); } //从上往下看的顺序序列,符合题目要求存入list3,作为一次排序的结果 Collections.reverse(list3); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); List list=new ArrayList<Integer>(); List list2=new ArrayList<Integer>(); List list3=new ArrayList<Integer>(); List <String> result=new ArrayList<String>();//存储所有组数的结果 int m=sc.nextInt(); int n=0,k=0; while(m!=0){ n=sc.nextInt(); k=sc.nextInt(); for(int i=0;i<2*n;i++){ list3.add(sc.nextInt()); } while(k!=0){//k次洗牌 ss.pokeSort(list, list2, list3); k--;} // 存储每组结果 result.add(list3.toString().replace("[", "").replace("]", "").replace(",", "")); m--; list3.clear(); } for(String str:result)//打印结果 System.out.println(str); } }
import java.util.Scanner;
public class Main{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
while(T>0){
int n=sc.nextInt();
int k=sc.nextInt();
int []p=new int[2*n];
for(int i=0;i<2*n;i++){
p[i]=sc.nextInt();
}
for(int j=0;j<k;j++){
int []z=new int[2*n];
for(int i=0;i<2*n;i++){z[i]=p[i];}
for(int i=0;i<2*n;i++){
if(i%2==0){p[2*n-1-i]=z[2*n-1-i/2];}
else{p[2*n-1-i]=z[n-(i+1)/2];}
}
}
for(int i=0;i<2*n-1;i++){
System.out.print(p[i]+" ");}
System.out.print(p[2*n-1]);
System.out.println();
T--;
}
}
}
//实际上就是一组数分成两半,从上往下,左选一个,右选一个,与从下往上,右选一个,左选一个结果一样 #include <iostream> #include <queue> using namespace std; int main() { int t; cin >> t; while(t--) { int n,k,tmp; cin >> n >> k; queue<int> data, left, right; for(int i = 0; i < 2 * n; i++) { cin >> tmp; data.push(tmp); } //洗牌k次 while(k--) { //模拟分堆 for(int i = 0; i < 2 * n; i++) { i < n ? left.push(data.front()) : right.push(data.front()); data.pop(); } //模拟洗牌 for(int i = 0; i < n; i++) { int tmp; tmp = left.front(); data.push(tmp); left.pop(); tmp = right.front(); data.push(tmp); right.pop(); } } while(!data.empty()) { cout << data.front() << " "; //data.size() > 1 ? cout << data.front() << " " : cout << data.front(); data.pop(); } cout << endl; } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input=new Scanner(System.in); int T=input.nextInt(); while(T-->0){ int n=input.nextInt(); int k=input.nextInt(); int[] a=new int[2*n]; for(int i=0; i<2*n; i++) a[i]=input.nextInt(); for(int i=0; i<k; i++) xiPai(a, n); for(int i=0; i<2*n-1; i++) System.out.print(a[i]+" "); System.out.println(a[2*n-1]); } } public static void xiPai(int[] a, int n){ int[] A=new int[n]; int[] B=new int[n]; for(int i=0; i<n; i++) A[i]=a[i]; for(int i=n; i<2*n; i++) B[i-n]=a[i]; int x=0, y=0; for(int i=0; i<2*n; i++){ if(i%2==0) a[i]=A[x++]; else a[i]=B[y++]; } } }
#include <iostream>
#include <vector>
#include <stack>
#include <sstream>
using namespace std;
class Shuffle{
vector<int> leftH, rightH, dock;
int num;
public:
Shuffle(int n):num(n){};
void drawing(int card){
dock.push_back(card);
}
void split(){
leftH.assign(dock.begin(), dock.begin()+num);
rightH.assign(dock.begin()+num, dock.end());
}
void shuffle(){
stack<int> stk;
for(int i=0; i<2*num; i++){
if(i==0 || i%2==0){
stk.push(*(rightH.end()-1));
rightH.pop_back();
}else{
stk.push(*(leftH.end()-1));
leftH.pop_back();
}
}
dock.clear();
for(int i=0; i<2*num; i++){
dock.push_back(stk.top());
stk.pop();
}
}
void printInfo(){
for(int i=0; i< 2*num-1; i++){
cout << dock[i] <<' ';
}
cout <<dock[2*num - 1]<<endl;
}
};
int main(){
int numOfTest;
int n, k;
cin >> numOfTest;
while(numOfTest--){
int tmp;
cin >> n >> k;
Shuffle game(n);
for(int i=0; i<2*n; i++){
cin >> tmp;
game.drawing(tmp);
}
while(k--){
game.split();
game.shuffle();
}
game.printInfo();
}
return 0;
}
#include <stdio.h> #include <stack> using namespace std; void fun(int a[201],int n){ stack<int> S1; stack<int> S2; int leng=n*2; for(int i=0;i<n;i++){ S1.push(a[i]); } for(int i=n;i<n*2;i++){ S2.push(a[i]); } for(int i=leng-1;i>=0;i--){ if(i%2==0){ a[i]=S1.top(); S1.pop(); } else{ a[i]=S2.top(); S2.pop(); } } } int main(){ int T; scanf("%d",&T); while(T--){ int n,k; scanf("%d%d",&n,&k); int a[201]; for(int i=0;i<n*2;i++){ scanf("%d",&a[i]); } while(k--){ fun(a,n); } for(int i=0;i<n*2;i++){ if(i==0) printf("%d",a[i]); else printf(" %d",a[i]); } printf("\n"); } return 0; }