Last active
August 29, 2015 13:57
-
-
Save torazuka/9463450 to your computer and use it in GitHub Desktop.
第19回オフラインリアルタイムどう書く(Java解答)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertEquals; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import org.junit.Test; | |
public class Nebasec { | |
public String solve(String input) { | |
List<Integer> defects = parse(input); | |
Set<Integer> reservedSet = new HashSet<>(); | |
List<Integer> result = new ArrayList<>(); | |
for (Integer defect : defects) { | |
List<Integer> tmp = getReserved(defect); | |
for (Integer reserved : tmp) { | |
if (reservedSet.add(reserved) == false | |
&& result.contains(reserved) == false) { | |
result.add(reserved); | |
} | |
} | |
} | |
for (Integer defect : defects) { | |
if (result.contains(defect)) { | |
result.remove(defect); | |
} | |
} | |
Collections.sort(result); | |
return format(result); | |
} | |
List<Integer> parse(String input) { | |
String[] split = input.split(","); | |
List<String> tmp = Arrays.asList(split); | |
List<Integer> result = new ArrayList<>(); | |
for (String string : tmp) { | |
result.add(Integer.valueOf(string)); | |
} | |
return result; | |
} | |
List<Integer> getReserved(Integer sec) { | |
List<Integer> result = new ArrayList<>(); | |
result.add(getNext(sec)); | |
result.add(getPrev(sec)); | |
int n = sec / 100; | |
if (n == 1) { | |
int mod = sec % 100 * 2; | |
result.add(200 + mod); | |
result.add(getPrev(200 + mod)); | |
result.add(getNext(200 + mod)); | |
} else if (n == 2) { | |
if (sec % 2 == 0) { // 200台の偶数 | |
int mod = sec % 200 / 2; | |
result.add(100 + mod); | |
result.add(300 + mod * 3); | |
result.add(getPrev(300 + mod * 3)); | |
result.add(getNext(300 + mod * 3)); | |
} else { | |
int div = (sec - 200) / 2; | |
result.add(100 + div); | |
result.add(getNext(100 + div)); | |
result.add(300 + div * 3 + 1); | |
result.add(getNext(300 + div * 3 + 1)); | |
} | |
} else if (n == 3) { | |
if (sec % 3 == 0) { // 300台の3の倍数 | |
int div = (sec - 300) / 3; | |
result.add(200 + div * 2); | |
result.add(400 + div * 4); | |
result.add(getPrev(400 + div * 4)); | |
result.add(getNext(400 + div * 4)); | |
} else { | |
int div = (sec - 300) / 3; | |
int mod = (sec - 300) % 3; | |
result.add(200 + div * 2 + mod - 1); | |
result.add(getNext(200 + div * 2 + mod - 1)); | |
result.add(400 + div * 4 + mod); | |
result.add(getNext(400 + div * 4 + mod)); | |
} | |
} else if (n == 4) { | |
if (sec % 4 == 0) { // 400台の4の倍数 | |
int div = (sec - 400) / 4; | |
result.add(300 + div * 3); | |
} else { | |
int div = (sec - 400) / 4; | |
int mod = (sec - 400) % 4; | |
result.add(300 + div * 3 + mod - 1); | |
result.add(getNext(300 + div * 3 + mod - 1)); | |
} | |
} | |
return result; | |
} | |
int getNext(int n) { | |
if (n == 107) { | |
return 100; | |
} | |
if (n == 215) { | |
return 200; | |
} | |
if (n == 323) { | |
return 300; | |
} | |
if (n == 431) { | |
return 400; | |
} | |
return n + 1; | |
} | |
int getPrev(int n) { | |
if (n == 100) { | |
return 107; | |
} | |
if (n == 200) { | |
return 215; | |
} | |
if (n == 300) { | |
return 323; | |
} | |
if (n == 400) { | |
return 431; | |
} | |
return n - 1; | |
} | |
String format(List<Integer> result) { | |
if (result.isEmpty()) { | |
return "none"; | |
} | |
StringBuilder sb = new StringBuilder(); | |
for (Integer integer : result) { | |
sb.append(String.valueOf(integer)); | |
sb.append(","); | |
} | |
return sb.toString().substring(0, sb.length() - 1); | |
} | |
@Test | |
public void testParse() throws Exception { | |
Nebasec target = new Nebasec(); | |
List<Integer> list0 = new ArrayList<>(); | |
list0.add(401); | |
List<Integer> list1 = new ArrayList<>(); | |
list1.add(401); | |
list1.add(402); | |
assertEquals(list1, target.parse("401,402")); | |
} | |
@Test | |
public void testFormat() throws Exception { | |
Nebasec target = new Nebasec(); | |
List<Integer> list0 = new ArrayList<>(); | |
assertEquals("none", target.format(list0)); | |
List<Integer> list1 = new ArrayList<>(); | |
list1.add(401); | |
assertEquals("401", target.format(list1)); | |
List<Integer> list2 = new ArrayList<>(); | |
list2.add(401); | |
list2.add(402); | |
assertEquals("401,402", target.format(list2)); | |
} | |
void test(String input, String expected) { | |
Nebasec target = new Nebasec(); | |
String result = target.solve(input); | |
// System.out.println(result.equals(expected) ? "OK" : input + " → " | |
// + result + " : ********NG********"); | |
assertEquals(expected, result); | |
} | |
@Test | |
public void testNebasec() throws Exception { | |
/* 0 */test("400,401,302", "300,301,402"); | |
/* 1 */test("105,100,306,414", "none"); | |
/* 2 */test("100", "none"); | |
/* 3 */test("211", "none"); | |
/* 4 */test("317", "none"); | |
/* 5 */test("414", "none"); | |
/* 6 */test("100,106", "107"); | |
/* 7 */test("205,203", "102,204"); | |
/* 8 */test("303,305", "304"); | |
/* 9 */test("407,409", "306,408"); | |
/* 10 */test("104,103", "207"); | |
/* 11 */test("204,203", "102,305"); | |
/* 12 */test("313,314", "209,418"); | |
/* 13 */test("419,418", "314"); | |
/* 14 */test("100,102,101", "201,203"); | |
/* 15 */test("103,206,309", "205,207,308,310"); | |
/* 16 */test("414,310,309", "206,311,413"); | |
/* 17 */test("104,102,206,307,102,202", "101,103,203,204,205,207,308"); | |
/* 18 */test("104,206,308,409,407", "103,205,207,306,307,309,408,410"); | |
/* 19 */test("313,406,213,301,409,422,412,102,428", "none"); | |
/* 20 */test("101,300,210,308,423,321,403,408,415", "none"); | |
/* 21 */test("304,316,307,207,427,402,107,431,412,418,424", "none"); | |
/* 22 */test("205,408,210,215,425,302,311,400,428,412", "none"); | |
/* 23 */test("200,311,306,412,403,318,427,105,420", "none"); | |
/* 24 */test("105,305,407,408,309,208,427", "104,209,306,406"); | |
/* 25 */test("311,304,322,404,429,305,316", "203,303,321,405,406,430"); | |
/* 26 */test("210,401,316,425,101", "211,315"); | |
/* 27 */test("414,403,404,416,428,421", "303,415"); | |
/* 28 */test("207,300,103,211,428", "104,206"); | |
/* 29 */test("322,314,310", "none"); | |
/* 30 */test("427,200,215", "100,323"); | |
/* 31 */test("311,402,424,307,318,430,323,305,201", | |
"200,204,301,302,306,322,423,425,431"); | |
/* 32 */test("425,430,408", "none"); | |
/* 33 */test("202,320,209,426", "319,427"); | |
/* 34 */test("430,209,302,310,304,431,320", "202,303,323"); | |
/* 35 */test("208,206,406,424,213,312", "207,311,313"); | |
/* 36 */test("420,302,313,413,317,402", "301,403"); | |
/* 37 */test("319,306,309,418,204,411", "305,307,308,412"); | |
/* 38 */test("400,308,105,430,203,428,209", "104,210,429,431"); | |
/* 39 */test("200,305,214", "215"); | |
/* 40 */test("214,408,410,407,317,422", "306,316,409,423"); | |
/* 追加 */test("212,319,213,320,107", "106,214,318,426"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
で行けると思います。