7-6 猴子選大王 (20分)

一羣猴子要選新猴王。新猴王的選擇方法是:讓N只候選猴子圍成一圈,從某位置起順序編號爲1~N號。從第1號開始報數,每輪從1報到3,凡報到3的猴子即退出圈子,接着又從緊鄰的下一隻猴子開始同樣的報數。如此不斷循環,最後剩下的一隻猴子就選爲猴王。請問是原來第幾號猴子當選猴王?

輸入格式:

輸入在一行中給一個正整數N(≤\le1000)。

輸出格式:

在一行中輸出當選猴王的編號。

輸入樣例:

11

輸出樣例:

7
import java.io.IOException;
import java.io.*;
import java.util.*;

class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}
	static char nextChar() throws IOException {
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
}

public class Main {
	public static ArrayList<Integer>list = new ArrayList<Integer>();
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int n = Reader.nextInt();
		for (int i = 1; i <= n; i++) {
			list.add(i);
		}
		int index = 0;
		while (list.size()!=1) {
			index = (index+2)%list.size();
			list.remove(index);
		}
		System.out.println(list.get(0));
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章