2. +
Problem
1 から 2n の数が書かれた 2n 枚のカードがあり,上から 1, 2,
3, ... , 2n の順に積み重なっている.
このカードを,次の方法を何回か用いて並べ替える.
整数 k でカット
上から k 枚のカードの山 A と 残りのカードの山 B に分けた後,
山 A の上に山 B をのせる.
3. リフルシャッフル
上から n 枚の山 A と残りの山 B に分け, 上から A の1枚目,
B の1枚目, A の2枚目, B の2枚目, …, A の n枚目, B の
n枚目, となるようにして, 1 つの山にする.
入力ファイルの指示に従い,カードを並び替えたあとのカード
の番号を,上から順番に出力するプログラムを作成せよ.
6. +
Main
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
CardDataStracture cds = new CardDataStracture(n);
int m = Integer.parseInt(sc.nextLine());
for(int i=0;i<m;i++){
int k = Integer.parseInt(sc.nextLine());
if(k==0){
cds.riffle();
}else{
cds.cut(k);
}
}
cds.print();
}
}
7. +
2つに山をカットした時の一つ目を
作る
カットしてできた二つの山を一つに
まとめる。この時、一つ目の山が後
ろに来るようにする。
public void cut(int k){
int[] A = new int[k];
int[] B = new int[2*n-k];
int i;
for(i=0;i<A.length;i++){
A[i]=card[i];
}
for(;i<2*n;i++){
B[i-k]=card[i];
}
int pointer=0;
for(i=0;i<B.length;i++){
card[pointer]=B[i];
pointer++;
}
for(i=0;i<A.length;i++){
card[pointer]=A[i];
pointer++;
}
}
2つに山をカットした時の二つ目を
作る
14. +
Main
public class Main {
public static void main(String[] args){
BinaryPool binaryPool = new BinaryPool(10);
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String[] strs = sc.nextLine().split(" ");
int n = Integer.parseInt(strs[0]);
int s = Integer.parseInt(strs[1]);
if(n==0&&s==0){
break;
}
System.out.println(new Execution(binaryPool,n).getPattern(s));
}
}
}
入力文字列を空白でsplit、
intに変換。s,nが共に0の時
ループを抜ける。