问题描述:名人问题
一个名人就是指这样一个人:所有其他人都认识他,并且他不认识任何其他人。现在有一个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. 百度翻译api 实现简易微信翻译小程序

    介绍 口袋翻译 口袋翻译 微信小程序 翻译功能 含7类语言的相互翻译 包含最近10条的翻译历史回溯功能 微信搜索:简e翻译 功能展示   使用百度翻译api需要申请 appid 与 key 并在 ap ...

  2. OpenCV学习5-----使用Mat合并多张图像

    最近做实验需要对比实验结果,需要将几张图片拼在一起,直观对比. 尝试用OpenCV解决. 核心思想其实是   声明一个足够大的,正好容纳下那几张图片的mat,然后将拼图依次copy到大图片相应的位置. ...

  3. windows远程连接失败问题排查思路

    一般情况下,对WIN7的远程连接只需要5步即可完成远程连接的设置: 1).用户是否设置了密码 2).计算机属性-允许远程登录 3).设置计算机永不睡眠 4).关闭防火墙或者设置入站规则 5).排查Re ...

  4. Median Weight Bead(最短路—floyed传递闭包)

    Description There are N beads which of the same shape and size, but with different weights. N is an ...

  5. oracle数据库之存储函数和过程

    一.引言     ORACLE 提供可以把 PL/SQL 程序存储在数据库中,并可以在任何地方来运行它.这样就叫存储过程或函数.过程和函数统称为 PL/SQL 子程序,他们是被命名的 PL/SQL 块 ...

  6. unordered_map(hash_map)和map的比较

    测试代码: #include <iostream> using namespace std; #include <string> #include <windows.h& ...

  7. 原生javascript自定义input[type=radio]效果

    2018年6月27日 更新 找到最为简单的仅仅使用css3的方案 <!DOCTYPE html> <html lang="en"> <head> ...

  8. 简介Kafka Streams

    本文从以下几个方面介绍Kafka Streams: 一. Kafka Streams 背景 二. Kafka Streams 架构 三. Kafka Streams 并行模型 四. Kafka Str ...

  9. WITH REPLACE 含义

    RESTORE DATABASE db_CSharp from disk='backup.bak' WITH REPLACE WITH REPLACE后面是限定条件,with replace意思是替换 ...

  10. linux下清空文件全部内容,如log日志

    在实际操作中经常需要清空log文件, 比如a.log,   有的人说, 直接删除不就行了, 但是, 直接删除后, 没法使用tail -f a.log了. 有的人说, 先rm再touch一个新文件不就可 ...