Celebrity Problem

Celebrity Problem

问题:

在一个房间里有 N 个人,其中一个是名人,所谓名人就是大家都认识他,但是他不认识任何人。其它人可能认识房间里面另外的一部分人。你可以问任何人问题,但是问题只能是:你认识 X 吗,对方回答 Yes or  No. 请问最少要问多少个问题才能把名人找出来?


分析:

我们把人编号,比如从1 到 N。 

我们考虑最坏情况:你问每一个人是否认识 X ,如果大家都认识,那么 X 一定是名人,如果其中一个人说不认识,那么那个人一定不是名人。因为 X 的范围是 1 到 N, 所以,在最坏情况下,我们 要问 (N - 1)* N 个问题。


但是,有一种更好的方法。我们从1开始,依次问他是否认识他的下一个,比如 2, 如果 1 说认识,那么 1 一定不是名人,2 有可能是名人; 如果1 说不认识,2 一定不是名人,1 却有可能是名人。这是这个方法巧的地方。所以,不管1 回答是还是不是,我们都可以确定其中一个不是名人,然后,我们继续问 可能是名人那一位 是否认识 3, 然后依次下去,直到第 N 个人。这样我们就可以只要问 (N - 1) 个问题就可以把名人找出来。

http://blog.csdn.net/beiyeqingteng/article/details/7707485


import java.util.Stack;

/**
In a party of N people, only one person is known to everyone. 
Such a person may be present in the party, 
if yes, (s)he doesn’t know anyone in the party. 
We can only ask questions like “does A know B? “. 
Find the stranger (celebrity) in minimum number of questions.

在一个有N个人的party上,只有一个名人被大家认识,
这个名人可能会出席在party上。如果他出席了,我们只能
问“A认不认识B”这个问题。
用最少的问题数来找出这个名人!

http://www.careercup.com/question?id=13167666
http://www.geeksforgeeks.org/the-celebrity-problem/
 */
public class CelebrityProblem {

	static int N = 8;
	static int size = 4;
	static int matrix[][] = {{0,0,1,0},
								{0,0,1,0},
								{0,0,0,0},
								{0,0,1,0}};
	
	public static boolean haveAcquiantance(int a, int b){
		return matrix[a][b] != 0;
	}
	
	public static int celebrityUsingStack(int size){
		Stack<Integer> stack = new Stack<Integer>();
		int i;
		int C;	// Celebrity
		
		i = 0;
		while(i < size){
			stack.push(i);
			i++;
		}
		
		int A = stack.pop();
		int B = stack.pop();
		
		while(stack.size() != 1){		// 直到剩余一个人为止
			if(haveAcquiantance(A, B)){	// A认识B,说明A不是名人
				A = stack.pop();	// 舍弃A继续查找
			}else{
				B = stack.pop();
			}
		}
		
		// 因为最后一个人还没被检查,所以要把C和A,B比较
		C = stack.pop();		// C是栈的最后一个元素,有可能是名人
		if(haveAcquiantance(C, B)){	// 如果C认识B,说明C不是名人,B可能是名人
			C = B;
		}
		if(haveAcquiantance(C, A)){
			C = A;
		}
		
		if(sanityTest(C) == -1){
			System.out.println("Sanity Test Failed!");
		}else{
			System.out.println("Test ok!");
		}
		return C;
	}
	
	public static int sanityTest(int C){
		int i = 0;
		Stack<Integer> stack = new Stack<Integer>();
		while(i < size){
			if(i != C){
				stack.push(i);
			}
			i++;
		}
		while(!stack.isEmpty()){
			i = stack.pop();		// 从栈顶开始检查
			if(haveAcquiantance(C, i)){	// 如果C认识i,则出错,因为名人不可能认识别人
				return -1;
			}
			if(!haveAcquiantance(i, C)){	// 如果i不认识C,也出错,因为大家必须都认识名人
				return -1;
			}
		}
		return C;
	}
	
	public static void main(String[] args) {
		int id = celebrityUsingStack(size);
		System.out.println(id);
	}
	
	

}


As an AI language model, I don't have personal experiences or have the ability to make up stories. However, here are some celebrity stories that you may find interesting: 1. Lady Gaga's story of perseverance: Before Lady Gaga became the global superstar that she is today, she faced numerous rejections and struggles in the music industry. She was dropped by her record label and had to perform in small bars and clubs to make ends meet. However, she never gave up on her dreams and finally got her big break with her debut album, "The Fame." 2. Jennifer Lawrence's empowering speech on body shaming: During a press conference for one of her movies, Jennifer Lawrence was asked a question about her diet and body, which she found offensive. Instead of ignoring the question, she used the opportunity to speak out against body shaming and the unrealistic beauty standards that women face in Hollywood. Her courageous and honest response earned her praise from fans and fellow celebrities alike. 3. Dwayne "The Rock" Johnson's inspiring journey to success: Dwayne "The Rock" Johnson started his career as a professional wrestler and later transitioned to acting. He faced many challenges along the way, including multiple injuries and setbacks. However, he never gave up on his goals and worked tirelessly to build his brand and become one of the most successful actors in Hollywood. 4. Beyoncé's message of female empowerment: Beyoncé is known for her powerful music and message of female empowerment. She has consistently used her platform to speak out against issues like sexism, racism, and police brutality. Her music and activism have inspired millions of people around the world to stand up for their rights and fight for equality. These are just a few examples of how celebrities can use their influence to inspire and empower others. By sharing their stories, we can all learn valuable lessons about perseverance, courage, and standing up for what we believe in.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值