RULES:
1). 唔準runtime改code, 改class, 改memory, 即係無得用我最愛的setAccessible()
2). 除左(1) 同題目既要求之外,你點寫都得,總之要做到
=============================================================
Introduction

This is the first in a series of Java puzzles, that put your Java skills to the test, in a challenging and fun way!
A puzzle consists of some given Java code with a line in it that seems to be impossible to reach. It’s up to you to find the hole in it, abuse a subtle behavior of Java to make execution reach that line anyways.
There almost aren’t any rules; any cheating inside your code is allowed; it is the whole point of the puzzle. [Clarification: Cheating the environment is not]. You must run with the security manager enabled (
java -Djava.security.manager), otherwise it would be too easy (with setAccessible for example). [Update: Use common sense, and see the exact rules if you're in doubt].The puzzle
How can you fit 20 clowns into a Volkswagen? Two classes are given: an empty Clown class, and a Volkswagen class to which you can add clowns. When you try to add a Clown, it is checked that it isn’t already full. But if you just try hard enough, there’s always room for some extra clowns…
1
2
3
4
| package clowns;public class Clown {} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| package clowns;import java.util.HashSet;import java.util.Set;public class Volkswagen { private static final int CAPACITY = 5; private Set<Clown> clowns = new HashSet<Clown>(); public synchronized void add(Clown clown) { if (clowns.size() >= CAPACITY) { throw new IllegalStateException("I'm full"); } else { clowns.add(clown); } } public synchronized void done() { if (clowns.size() == 20) { // The goal is to reach this line System.out.println("I'm a Volkswagen with 20 clowns!"); } }} |
Write a class that when executed pushes 20 clowns into the little car, and reaches the marked line. Here is one that won’t really work, just to get you started:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| package you;import clowns.Clown;import clowns.Volkswagen;public class You { public static void main(String args[]) { // TODO put 20 clowns into a Volkswagen Volkswagen vw = new Volkswagen(); for (int i = 0; i < 20; i++) { vw.add(new Clown()); } vw.done(); }} |
=============================================================
PS: 這不是我出的題目,出題目既堅係神人
PSS: 真係好tricky
PSSS: 佢既下一條都好屈機
refs: http://wouter.coekaerts.be/2012/puzzle-clowns
No comments:
Post a Comment