자바- 647 533 그러나 Java 8 Streams에 대한 일부 브라우니 포인트를 기대합니다.
class I{int p;I e;I(int p,I e){this.p=p;this.e=e;}void x(){if(e!=null){int r=p&1;e.p+=(p-r)/2;p=r;}}public String toString(){return ""+p;}}Deque<I>B(String d){H<I>e=new H<>();return Arrays.stream(d.split(" ")).map(s->Integer.valueOf(s)).map(p->e.hold(new I(p,e.held()))).collect(Collectors.toCollection(LinkedList::new));}void x(Deque<I>is){is.descendingIterator().forEachRemaining((I i)->{i.x();});}void t(String s){Deque<I> a=B(s);x(a);System.out.println(a);}class H<T>{T h=null;H(){}T hold(T t){return (h=t);}T held(){return h;}}
비 압축 형태 :
private static class Island {
int population;
final Island eastwardIsland;
Island(int population, Island eastwardIsland) {
this.population = population;
this.eastwardIsland = eastwardIsland;
}
private void exodus() {
if (eastwardIsland != null) {
// How many remain.
int remain = population & 1;
// How many leave.
int leave = population - remain;
// Account for 50% death rate.
int arrive = leave / 2;
// Modify the eastward island population.
eastwardIsland.population += arrive;
// Change my population.
population = remain;
}
}
@Override
public String toString() {
return String.valueOf(population);
}
}
private Deque<Island> buildIslands(String data) {
// Holds the island to the east as we traverse.
final Holder<Island> eastward = new Holder<>();
// Build my list of islands - assumes order is retained.
return Arrays.stream(data.split(" "))
// Convert to int.
.map(s -> Integer.valueOf(s))
// Build the island in a chain.
.map(p -> eastward.hold(new Island(p, eastward.held())))
// Roll them into a linked list.
.collect(Collectors.toCollection(LinkedList::new));
}
private void exodus(Deque<Island> islands) {
// Walk backwards.
islands.descendingIterator()
// Perform all exodus.
.forEachRemaining((Island i) -> {
i.exodus();
});
}
private void test(String data) {
Deque<Island> archipelago = buildIslands(data);
// Initiate the exodus.
exodus(archipelago);
// Print them.
System.out.println(archipelago);
}
다음의 도움으로 :
// Mutable final.
private static class Holder<T> {
private T held = null;
public Holder() {
}
public Holder(T it) {
held = it;
}
public T hold(T it) {
return (held = it);
}
public T held() {
return held;
}
@Override
public String toString() {
return held == null ? "null" : held.toString();
}
}
@DavidCarraher의 테스트가 약간 걱정되었습니다.
145 144 144 59 35 129 109 99 200 24 219 96 12 121 7520 153 124 131 178 228 120 63 207 228
생성
270, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0