问题描述:名人问题
一个名人就是指这样一个人:所有其他人都认识他,并且他不认识任何其他人。现在有一个N个人的集合,以及他们之间的认识关系。求一个算法找出其中的名人(如果有的话)或者判断出没有名人(如果没有的话)。

1.构造输入数据数组,名人所在的Index可控。

public static int[,] initCelebrityData(int size, int celebrateIndex)
{
int[,] arrray = new int[size, size];
Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = ; i < size; i++)
{
int tmpColumn = rnd.Next(, size - );
for (int j = ; j < size; j++)
{
if (j == tmpColumn)
{
arrray[i, j] = ;//每一行至少有一个元素为1,每个人至少认识一个人
}
else
{
arrray[i, j] = rnd.Next() % ;
}
}
}
//构造一个名流
for (int i = ; i < size; i++)
{
arrray[celebrateIndex, i] = ; //Celebrity know nobody.
arrray[i, celebrateIndex] = ;//Everybody knows the Celebrity.
}
return arrray;
}

2.求解名人,分两步,第一步排除所有非名人,第二步判断剩下的候选者是否为名人。

  public static bool FindCelebrity(int[,] knowArray, ref int celebrityIndex)
{
if (knowArray == null) throw new ArgumentNullException("knowArray");
if (knowArray.GetUpperBound() != knowArray.GetUpperBound()) throw new Exception("A square matrix is required.");
int i = ;//candidate
for (int j = ; j <= knowArray.GetUpperBound(); j++)
{
if (knowArray[i, j] == )//i know j
i = j;
}
bool result = true; for (int j = ; j <= knowArray.GetUpperBound(); j++)
{
if (i == j) continue;
if (knowArray[i, j] == || knowArray[j, i] == )//i know j
{
result = false;
break;
}
} if (result)
{ celebrityIndex = i; }
else
{ celebrityIndex = -; }
return result;
}
}

运行结果:

下载源码

作者:Andy Zeng
欢迎任何形式的转载,但请务必注明出处。

http://www.cnblogs.com/andyzeng/p/3670383.html

名人问题/名流问题/Celebrity的更多相关文章

  1. 100-days: eight

    Title: U.S.(美国司法部)  accuses rich parents of college entry fraud accuse  v.指控,指责,谴责 accuse someone of ...

  2. [LeetCode] Find the Celebrity 寻找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  3. [Swift]LeetCode277. 寻找名人 $ Find the Celebrity

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  4. 识别名人 · Find the Celebrity

    [抄题]: 假设你和 n 个人在一个聚会中(标记为 0 到 n - 1),其中可能存在一个名人.名人的定义是所有其他 n - 1 人都认识他/她,但他/她不知道任何一个.现在你想要找出这个名人是谁或者 ...

  5. [leetcode]277. Find the Celebrity 找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  6. 名人问题 算法解析与Python 实现 O(n) 复杂度 (以Leetcode 277. Find the Celebrity为例)

    1. 题目描述 Problem Description Leetcode 277. Find the Celebrity Suppose you are at a party with n peopl ...

  7. [LeetCode] 277. Find the Celebrity 寻找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  8. [leetcode]277. Find the Celebrity谁是名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  9. Find celebrity

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

随机推荐

  1. vmware安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题

    虚拟机使用的是VMware Workstation,并且首次在虚拟机体验64 位系统.在新建好虚拟机,运行时候就出现了VMware Workstation 的提醒:此主机支持 Intel VT-x,但 ...

  2. nginx配置和网站的部署

    环境: CentOS Linux release 7.3.1611 (Core) nginx version: nginx/1.13.4 PHP 5.4.16 (cli) (built: Nov 6 ...

  3. JSR303中的来验证数据信息

    spring mvc之实现简单的用户管理三 博客分类: spring spring mvc spring mvc dispatcherServlet springspring mvcbean vali ...

  4. Huffuman树

    问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, pn-1},用这列数构造Huffman树的过程如下: 1. ...

  5. <Android>资源的访问,颜色、字符串、尺寸、XML、DRAWABLES资源分使用

    1.资源的访问 代码中使用Context的getResources()方法得到Resources对象,访问自己定义的资源R.资源文件类型.资源文件名称,访问系统定义的资源android.R. 资源文件 ...

  6. 201621044079 韩烨 week11-作业11-多线程

    作业11-多线程 参考资料 多线程参考文件 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序 ...

  7. HDU 1995 R-汉诺塔V

    https://vjudge.net/contest/67836#problem/R 用1,2,...,n表示n个盘子,称为1号盘,2号盘,....号数大盘子就大.经典的汉诺塔问 题经常作为一个递归的 ...

  8. server2003 必要的系统优化和安全设置

    修改远程桌面端口: Windows 2003系统中的远程终端服务是一项功能非常强大的服务,同时也成了入侵者长驻主机的通道,入侵者可以利用一些手段得到管理员账号和密码并入侵主机.下面,我们来看看如何通过 ...

  9. [计算机网络-应用层] FTP协议

    文件传输协议:FTP 如下图所示:用户通过一个FTP用户代理与FTP交互.该用户首先提供远程主机的主机名,使本地主机的FTP客户机进程建立一个到远程主机FTP服务器进程的TCP连接.然后,该用户提供用 ...

  10. java read读取方法 之 指定读取大小,根据返回值判断是否读取所有的内容 -1表示读取完毕

    当读取的内容小于指定的长度时候 字符数组里面会出现一部分字符是上一次遗留下来的情况