【Java】あみだくじ_Ver2
コード更新致しました。
固有処理基底クラスを更新したので、更新したコードを紹介いたします。
import static java.lang.Integer.parseInt;
import static java.lang.System.out;
import volume0.BaseExe;
public class DrawingLots extends BaseExe {
public static void main(String[] args) {
new DrawingLots().exeSysIn();
}
@Override
protected void execute() throws Exception {
int[] lots = getLots(parseInt(nextLine()));
int crossCount = parseInt(nextLine());
for (int chageCount = 0; chageCount < crossCount; chageCount++) {
changeLot(nextLine().split(","), lots);
}
for (int lot : lots) {
out.println(lot);
}
}
private int[] getLots(int count) {
int[] lots = new int[count];
for (int lotIndex = 0; lotIndex < count; lotIndex++) {
lots[lotIndex] = lotIndex + 1;
}
return lots;
}
private void changeLot(String[] changeNumbers, int[] lots) {
int beforeIndex = parseInt(changeNumbers[0]) - 1;
int afterIndex = parseInt(changeNumbers[1]) - 1;
int tmp = lots[beforeIndex];
lots[beforeIndex] = lots[afterIndex];
lots[afterIndex] = tmp;
}
}
テストドライバは以下になります。
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import volume0.BaseTest;
public class DrawingLotsTest extends BaseTest {
private DrawingLots dl;
@Before
public void setUp() throws Exception {
super.setUp();
dl = new DrawingLots();
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void test() {
dl.exeFileIn("./data/volume0_0011/in.txt");
assertOutList("./data/volume0_0011/out.txt");
}
}
テストドライバ内で使用している「in.txt」・「out.txt」の内容は以下になります。
in.txt
5
4
2,4
3,5
1,2
3,4
out.txt
4
1
2
5
3
Ver1で問題の解説等を行なっているので、そちらも参照してください。
アルゴリスムで困った時は、以下の本を参考にしてます。Javaに置き換えるのが少々難解ですが、、、
固有処理の基底クラスは以下の記事に記載していますので、参考にしてください。
テストドライバで継承しているクラスは以下の記事に記載していますので、参考にしてみてください。