Created
April 6, 2013 16:06
-
-
Save torazuka/5326603 to your computer and use it in GitHub Desktop.
第9回オフラインリアルタイムどう書く(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
package yhpg9; | |
import static org.junit.Assert.assertEquals; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.junit.Test; | |
/** | |
* 問題: http://nabetani.sakura.ne.jp/hena/ord9busfare/ | |
*/ | |
public class Busfee { | |
private final boolean isAdult(final String string) { | |
return string.charAt(0) == 'A'; | |
} | |
private final boolean isInfant(final String string) { | |
return string.charAt(0) == 'I'; | |
} | |
private final boolean hasPass(final String string) { | |
return string.charAt(1) == 'p'; | |
} | |
private final boolean hasWelfare(final String string) { | |
return string.charAt(1) == 'w'; | |
} | |
public String solve(final String input) { | |
int base = Integer.valueOf(input.split(":")[0]); | |
String passenger = input.split(":")[1]; | |
List<String> passengers = Arrays.asList(passenger.split(",")); | |
int adultNum = countAdult(passengers); | |
int result = 0; | |
int freeNum = 0; | |
List<String> welfareInfants = new ArrayList<>(); | |
for (int i = 0; i < passengers.size(); i++) { | |
String one = passengers.get(i); | |
if (isInfant(one) && freeNum < adultNum * 2) { | |
if (hasPass(one)) { | |
continue; | |
} | |
if (hasWelfare(one)) { | |
welfareInfants.add(one); | |
continue; | |
} | |
freeNum++; | |
continue; | |
} | |
result += getFee(one, base); | |
} | |
for (String each : welfareInfants) { | |
if (freeNum < adultNum * 2) { | |
freeNum++; | |
continue; | |
} | |
result += getFee(each, base); | |
} | |
return String.valueOf(result); | |
} | |
private int getFee(final String target, final int base) { | |
int tmp = 0; | |
if (isAdult(target)) { | |
tmp = base; | |
} else { | |
tmp = getHalf(base); | |
} | |
return discount(target, tmp); | |
} | |
private int discount(final String target, final int original) { | |
if (hasPass(target)) { | |
return 0; | |
} else if (hasWelfare(target)) { | |
return getHalf(original); | |
} | |
return original; | |
} | |
private int getHalf(final int n) { | |
int tmp = n / 2; | |
if (tmp % 10 == 0) { | |
return tmp; | |
} | |
return tmp + (10 - tmp % 10); | |
} | |
private int countAdult(final List<String> passengers) { | |
int result = 0; | |
for (String each : passengers) { | |
if (isAdult(each)) { | |
result++; | |
} | |
} | |
return result; | |
} | |
private void test(final String input, final String expected) { | |
Busfee bf = new Busfee(); | |
assertEquals(expected, bf.solve(input)); | |
} | |
@Test | |
public void testBusfee() throws Exception { | |
/* 0 */test("210:Cn,In,Iw,Ap,Iw", "170"); | |
/* 1 */test("220:Cp,In", "110"); | |
/* 2 */test("230:Cw,In,Iw", "240"); | |
/* 3 */test("240:In,An,In", "240"); | |
/* 4 */test("250:In,In,Aw,In", "260"); | |
/* 5 */test("260:In,In,In,In,Ap", "260"); | |
/* 6 */test("270:In,An,In,In,Ip", "410"); | |
/* 7 */test("280:Aw,In,Iw,In", "210"); | |
/* 8 */test("200:An", "200"); | |
/* 9 */test("210:Iw", "60"); | |
/* 10 */test("220:Ap", "0"); | |
/* 11 */test("230:Cp", "0"); | |
/* 12 */test("240:Cw", "60"); | |
/* 13 */test("250:In", "130"); | |
/* 14 */test("260:Cn", "130"); | |
/* 15 */test("270:Ip", "0"); | |
/* 16 */test("280:Aw", "140"); | |
/* 17 */test("1480:In,An,In,In,In,Iw,Cp,Cw,In,Aw,In,In,Iw,Cn,Aw,Iw", | |
"5920"); | |
/* 18 */test("630:Aw,Cw,Iw,An,An", "1740"); | |
/* 19 */test("340:Cn,Cn,Ip,Ap", "340"); | |
/* 20 */test("240:Iw,Ap,In,Iw,Aw", "120"); | |
/* 21 */test("800:Cw,An,Cn,Aw,Ap", "1800"); | |
/* 22 */test("1210:An,Ip,In,Iw,An,Iw,Iw,An,Iw,Iw", "3630"); | |
/* 23 */test("530:An,Cw,Cw", "810"); | |
/* 24 */test("170:Aw,Iw,Ip", "90"); | |
/* 25 */test("150:In,Ip,Ip,Iw,In,Iw,Iw,In,An,Iw,Aw,Cw,Iw,Cw,An,Cp,Iw", | |
"580"); | |
/* 26 */test("420:Cn,Cw,Cp", "320"); | |
/* 27 */test("690:Cw,In,An,Cp,Cn,In", "1220"); | |
/* 28 */test("590:Iw,Iw,Cn,Iw,Aw,In,In,Ip,Iw,Ip,Aw", "1200"); | |
/* 29 */test("790:Cw,Cn,Cn", "1000"); | |
/* 30 */test("1220:In,In,An,An,In,Iw,Iw,In,In,Ip,In,An,Iw", "4590"); | |
/* 31 */test("570:Cw,Cn,Cp", "440"); | |
/* 32 */test("310:Cn,Cw,An,An,Iw,Cp,Cw,Cn,Iw", "1100"); | |
/* 33 */test("910:Aw,In,Iw,Iw,Iw,Iw,Iw,An,Cw,In", "2290"); | |
/* 34 */test("460:Iw,Cw,Cw,Cn", "590"); | |
/* 35 */test("240:Iw,Iw,In,Iw,In,In,Cn,In,An", "780"); | |
/* 36 */test("1240:In,In,In,Ap,In,Cw,Iw,Iw,Iw,Aw,Cw", "2170"); | |
/* 37 */test( | |
"1000:Iw,Ip,In,An,In,In,In,An,In,Iw,In,In,Iw,In,Iw,Iw,Iw,An", | |
"5500"); | |
/* 38 */test("180:In,Aw,Ip,Iw,In,Aw,In,Iw,Iw,In", "330"); | |
/* 39 */test("440:In,Ip,Cp,Aw,Iw,In,An", "660"); | |
/* 40 */test("1270:Ap,In,An,Ip,In,Ip,Ip", "1270"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment