Skip to content

Instantly share code, notes, and snippets.

@torazuka
Created March 18, 2013 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torazuka/5186764 to your computer and use it in GitHub Desktop.
Save torazuka/5186764 to your computer and use it in GitHub Desktop.
第9回オフラインリアルタイムどう書く参考問題(Java解答)
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.Test;
/**
* 問題: http://nabetani.sakura.ne.jp/hena/ord9nummake/
*/
public class NumberMake {
public String solve(String input) {
int order = Integer.valueOf(input.split(":")[0]);
String cards = input.split(":")[1];
Set<String> cardsSet = getCardsSet(cards);
Set<String> cardSet = new HashSet<>();
for (String each : cardsSet) {
Set<String> tmpSet = makeNum("", each);
cardSet.addAll(tmpSet);
}
cardSet = checkZero(cards, cardSet);
List<String> cardList = new ArrayList<>(cardSet);
Collections.sort(cardList);
if (cardList.size() < order) {
return "-";
}
return cardList.get(order - 1);
}
protected Set<String> getCardsSet(final String cards) {
Set<String> result = new HashSet<>();
result.add(cards);
String tmp = cards;
for (int i = 0; i < getSixNum(cards); i++) {
tmp = tmp.replaceFirst("6", "9");
result.add(tmp);
}
return result;
}
protected int getSixNum(String cards) {
int result = 0;
for (int i = 0; i < cards.length(); i++) {
if (cards.charAt(i) == '6') {
result++;
}
}
return result;
}
protected Set<String> makeNum(final String card, String cards) {
Set<String> result = new HashSet<>();
for (int i = 0; i < cards.length(); i++) {
String tmpCard = card;
tmpCard += cards.charAt(i);
String next = "";
if (0 < i) {
next += cards.substring(0, i);
}
if (i < cards.length() - 1) {
next += cards.substring(i + 1);
}
// System.out.println("card: " + tmpCard + ", next: " + next);
if (tmpCard.length() < 4) {
Set<String> tmp = makeNum(tmpCard, next);
result.addAll(tmp);
} else {
result.add(tmpCard);
}
}
return result;
}
protected Set<String> checkZero(String cards, Set<String> cardList) {
Set<String> result = new HashSet<>();
for (String string : cardList) {
if (string.startsWith("0") == false) {
result.add(string);
}
}
return result;
}
protected void test(String input, String expected) {
NumberMake nm = new NumberMake();
assertEquals(expected, nm.solve(input));
}
@Test
public void testName() throws Exception {
/* 0 */test("13:01167", "1109");
/* 1 */test("1:1234", "1234");
/* 2 */test("2:1234", "1243");
/* 3 */test("7:1234", "2134");
/* 4 */test("24:1234", "4321");
/* 5 */test("25:1234", "-");
/* 6 */test("2:1116", "1119");
/* 7 */test("2:0116", "1019");
/* 8 */test("3:6666", "6696");
/* 9 */test("16:6666", "9999");
/* 10 */test("10:01267", "1097");
/* 11 */test("14:111167", "1671");
/* 12 */test("1:1111", "1111");
/* 13 */test("2:1111", "-");
/* 14 */test("1:666633", "3366");
/* 15 */test("72:666611", "9999");
/* 16 */test("73:666622", "-");
/* 17 */test("1:666600", "6006");
/* 18 */test("52:666600", "9999");
/* 19 */test("53:666600", "-");
/* 20 */test("25:06688", "8086");
/* 21 */test("93:02566", "6502");
/* 22 */test("11:06688", "6809");
/* 23 */test("169:01268", "9801");
/* 24 */test("174:01268", "9821");
/* 25 */test("66:012288", "8201");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment