LoginSignup
0
0

More than 5 years have passed since last update.

第6回オフラインリアルタイムどう書くの解答(Java)

Posted at
KinshipOnlyMother.java
import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

/**
 * 問題: http://qiita.com/items/5e1c944541f09f0f9711
 * 鍋谷さんが解説してくださった模範解答のロジックをJavaで書きました。(自分の母の情報だけを持つVer.)
 */
public class KinshipOnlyMother {

    Map<Integer, Integer> mothers = new HashMap<>();

    public KinshipOnlyMother() {
        for (int i = 1; i < 41; i++) {
            mothers.put(i, (i + 1) / 3);
        }
    }

    protected String solve(String input) {
        int indexOf = input.indexOf('-');
        int in0 = Integer.parseInt(input.substring(0, indexOf));
        int in1 = Integer.parseInt(input.substring(indexOf + 2));

        if (in0 == in1) {
            return "me";
        } else if (mothers.get(in0) == in1) {
            return "mo";
        } else if (mothers.get(in1) == in0) {
            return "da";
        } else if (mothers.get(in0) == mothers.get(in1)) {
            return "si";
        } else if (mothers.get(mothers.get(in0)) == mothers.get(in1)) {
            return "au";
        } else if (mothers.get(mothers.get(in1)) == mothers.get(in0)) {
            return "ni";
        } else if (mothers.get(mothers.get(in0)) == mothers.get(mothers
                .get(in1))) {
            return "co";
        }
        return "-";
    }

    public static void main(String[] args) {
        KinshipOnlyMother kinship = new KinshipOnlyMother();
        kinship.solve("");
    }

    protected void test(String s0, String s1) {
        KinshipOnlyMother kinship = new KinshipOnlyMother();
        assertEquals(s1, kinship.solve(s0));
    }

    @Test
    public void testSolve() throws Exception {
        /* #0 */test("5->2", "mo");
        /* #1 */test("28->10", "au");
        /* #2 */test("1->1", "me");
        /* #3 */test("40->40", "me");
        /* #4 */test("27->27", "me");
        /* #5 */test("7->2", "mo");
        /* #6 */test("40->13", "mo");
        /* #7 */test("9->3", "mo");
        /* #8 */test("4->1", "mo");
        /* #9 */test("1->3", "da");
        /* #10 */test("12->35", "da");
        /* #11 */test("3->8", "da");
        /* #12 */test("6->19", "da");
        /* #13 */test("38->40", "si");
        /* #14 */test("9->8", "si");
        /* #15 */test("4->2", "si");
        /* #16 */test("15->16", "si");
        /* #17 */test("40->12", "au");
        /* #18 */test("10->4", "au");
        /* #19 */test("21->5", "au");
        /* #20 */test("8->2", "au");
        /* #21 */test("3->5", "ni");
        /* #22 */test("11->39", "ni");
        /* #23 */test("2->13", "ni");
        /* #24 */test("13->32", "ni");
        /* #25 */test("14->22", "co");
        /* #26 */test("40->34", "co");
        /* #27 */test("5->8", "co");
        /* #28 */test("12->10", "co");
        /* #29 */test("1->27", "-");
        /* #30 */test("8->1", "-");
        /* #31 */test("12->22", "-");
        /* #32 */test("2->40", "-");
        /* #33 */test("32->31", "-");
        /* #34 */test("13->14", "-");
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0