Skip to content

Instantly share code, notes, and snippets.

@yuki2006
Created September 28, 2013 13:02
Show Gist options
  • Save yuki2006/6741868 to your computer and use it in GitHub Desktop.
Save yuki2006/6741868 to your computer and use it in GitHub Desktop.
順列を求める再起関数のサンプル
public void hoge(){
int N = 10;//順列を求めるサイズ
int[] hit = new int[N];
int[] list = new int[N];
for (int i = 0; i < list.length; i++) {
list[i] = i + 1;
}
permutaion(hit, list, N);
}
// 順列を求める再起関数
private void permutaion(int hit[], int list[], int count) {
if (count == 0) {
int sum = 0;
for (int i = 0; i < hit.length; i++) {
// TODO ここに順列として与えられたものを処理する
list[hit[i] - 1]
}
} else {
int[] tmp = hit.clone();
for (int i = 0; i < list.length; i++) {
if (tmp[i] == 0) {
tmp[i] = count;
//再起によびだす
permutaion(tmp, list, count - 1);
tmp = hit.clone();
}
}
return c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment