A couple guys here at work have been working on some puzzles. They didn't like my solution to the 3rd puzzle, as they wanted it to be more math base and not like programming. Here are the puzzles:
Assume: a and b are positive integers. (
I think this condition only matters for the third puzzle)
1. Given a and b, how do you swap the values without using a third temporary variable?
2. Given a, what is the function to make b = a+1 if a is odd, and b=a-1 if a is even? You are not allowed to use conditional statements.
3. Given a and b, what is the function to make c=max(a,b) without using conditional statements?
Posted by
Jon Daley on
October 6, 2004, 11:11 am
| Read 2607 times
Category
General:
[
first]
[
previous]
[
next]
[
newest]
There are three guys in a gun-fight, one can shoot perfectly, one guy misses one out of three times, and you, who misses two out of three times.
This is an organized fight, so only one person shoots at a time, and since you can't shoot that well, you get to go first.
Assumption 1: You want to have the best chance of walking away alive.
Assumption 2: It only takes one successful hit to kill a guy
Where do you aim?
The fight goes from person to person, they can shoot wherever they want, once, and then the next guy goes. You keep taking turns until there is one person left.
Instead of shooting yourself, I say you aim into the air, and hit no one./>
A = the guy who never misses; B = the guy who hits 2 out of 3 shots. I'm assuming B shoots second, since we seem to be going in reverse order of competence. But I don't think it matters./>
You don't want to shoot B, because if you hit him, you're dead: It's A's turn to shoot, he never misses, and you are his only target. But if you kill A, B will then keep shooting at you, and he is twice the shot you are - pretty bad odds./>
Neither A nor B want to shoot at you while the other is alive, since you're such a bad shot. B can't take your way out, because A is bound to shoot him, fatally. So he has to shoot at A. If A dies, you are back where you would have been had you killed A, with this important difference: you get to shoot at B before he can shoot at you./>
If B does not kill A, A's best bet is still to kill B. Then you get at least once chance to kill A before he can kill you./>
Sounds like a pretty dangerous situation no matter how you look at it.
Your answer is correct. The original problem said the best guy went second, but after we thought about it, we decided it didn't matter.