首页 > 专题系列 > Java解POJ > POJ 1781 In Danger [解题报告] Java
2013
11-10

POJ 1781 In Danger [解题报告] Java

In Danger

问题描述 :

Flavius Josephus and 40 fellow rebels were trapped by the Romans. His companions preferred suicide to surrender, so they decided to form a circle and to kill every third person and to proceed around the circle until no one was left. Josephus was not excited by the idea of killing himself, so he calculated the position to be the last man standing (and then he did not commit suicide since nobody could watch).

We will consider a variant of this “game” where every second person leaves. And of course there will be more than 41 persons, for we now have computers. You have to calculate the safe position. Be careful because we might apply your program to calculate the winner of this contest!

输入:

The input contains several test cases. Each specifies a number n, denoting the number of persons participating in the game. To make things more difficult, it always has the format “xyez” with the following semantics: when n is written down in decimal notation, its first digit is x, its second digit is y, and then follow z zeros. Whereas 0<=x,y<=9, the number of zeros is 0<=z<=6. You may assume that n>0. The last test case is followed by the string 00e0.

输出:

For each test case generate a line containing the position of the person who survives. Assume that the participants have serial numbers from 1 to n and that the counting starts with person 1, i.e., the first person leaving is the one with number 2. For example, if there are 5 persons in the circle, counting proceeds as 2, 4, 1, 5 and person 3 is staying alive.

样例输入:

05e0
01e1
42e0
66e6
00e0

样例输出:

3
5
21
64891137

解题代码:

//* @author: [email protected]
import java.io.*;
public class Main
{
 public static void main(String[] args) throws NumberFormatException, IOException
 {
	InputStreamReader is=new InputStreamReader(System.in);
	BufferedReader in=new BufferedReader(is);
	while(true)
	{
        String s=in.readLine();
	 if(s.equals("00e0")) break;
	 int a=(int)(((s.charAt(0)-'0')*10+s.charAt(1)-'0')*Math.pow(10, s.charAt(3)-'0'));
	 s=Integer.toBinaryString(a);
	 s=s.substring(1)+s.charAt(0);
	 a=Integer.parseInt(s, 2);
	 System.out.println(a);
	}
  }
}

  1. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的