|
kasama |
|
単純に数え上げことしかできませんでした。あまり芸がないので、プログラムを作成してみました。
5段の階段では1往復の仕方は41通りあります。 6段の階段では1往復の仕方は99通りあります。 7段の階段では1往復の仕方は239通りあります。 8段の階段では1往復の仕方は577通りあります。 9段の階段では1往復の仕方は1393通りあります。 10段の階段では1往復の仕方は3363通りあります。 ですね。 public class Question345 { private int count = 0; public int walk(int n) { boolean step[] = new boolean[n]; for (int i = 0; i < step.length; ++i) step[i] = false; go(step, 0); for (int i = 0; i < step.length; ++i) step[i] = false; go(step, 1); return count; } private void go(boolean[] step, int n) { if (n == step.length - 1) { step[n] = true; back(copy(step), n - 1); back(copy(step), n - 2); return; } else if (n >= step.length) { return; } step[n] = true; go(copy(step), n + 1); go(copy(step), n + 2); } private void back(boolean[] step, int n) { if (n == -1) { boolean all = true; for (int i = 0; i < step.length; ++i) { if (!step[i]) { all = false; break; } } if (all) ++count; return; } else if (n < -1) { return; } step[n] = true; back(copy(step), n - 1); back(copy(step), n - 2); } private boolean[] copy(boolean[] step) { boolean[] stepCopy = new boolean[step.length]; for (int i = 0; i < step.length; ++i) stepCopy[i] = step[i]; return stepCopy; } public static void main(String args[]) { for (int i = 5; i <= 10; ++i) { int count = new Question345().walk(i); System.err.println(i + "段の階段では1往復の仕方は" + count + "通りあります。"); } } } |
|
和歌山
3月13日(木) 0:53:29
MAIL:kasama@s34.co.jp 18260 |