问题描述:名人问题
一个名人就是指这样一个人:所有其他人都认识他,并且他不认识任何其他人。现在有一个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. 【QT】常用类

    官方文档 doc QWidget QWidget类是所有用户界面对象的基类. 窗口部件是用户界面的一个基本单元:它从窗口系统接收鼠标.键盘和其它事件,并且在屏幕上绘制自己. 每一个窗口部件都是矩形的, ...

  2. 《机器学习实战》笔记——决策树(ID3)

    现在要介绍的是ID3决策树算法,只适用于标称型数据,不适用于数值型数据. 决策树学习算法最大的优点是,他可以自学习,在学习过程中,不需要使用者了解过多的背景知识.领域知识,只需要对训练实例进行较好的标 ...

  3. Some good articles

    https://alligator.io/vuejs/introduction-render-functions/ https://alligator.io/vuejs/vue-jwt-pattern ...

  4. vue.js学习之 打包为生产环境后,页面为白色

    vue.js学习之 打包为生产环境后,页面为白色 一:配置问题 当我们将项目打包为生产环境后,在dist文件夹下打开index.html,会发现页面为白色. 1:打开config>index.j ...

  5. 什么是Frozen Binary

    对于Python来说,你可以将Python的字节码,PVM(也就是解析器),以及需要的相关类库,打包成一个package,这个package实际上是一个二进制可执行文件,这样,用户获取到这个packa ...

  6. 《剑指Offer》题三十一~题四十

    三十一.栈的压入.弹出序列 题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的数字均不相等.例如,序列{1, 2, 3, 4 ,5}是某栈的压栈序列 ...

  7. 每周psp-第五周

    PSP表格: 类别 任务 开始时间 结束时间 中断时间 delta时间 开会 scrum立会 10.13下午6:04 10.13下午6:34 0 30 开会 scrum立会 10.14下午6:02 1 ...

  8. 20172332 实验一《Java开发环境的熟悉》实验报告

    20172332 2017-2018-2 <程序设计与数据结构>实验一报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 于欣月 学号:20172332 实验教师:王 ...

  9. JavaScript初探系列之String的基本操作

    1.字符串转换 字符串转换是最基础的要求和工作,你可以将任何类型的数据都转换为字符串,你可以用下面三种方法的任何一种: var myStr = num.toString(); // "19& ...

  10. Alpha冲刺——第四天

    Alpha第四天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...