问题描述:名人问题
一个名人就是指这样一个人:所有其他人都认识他,并且他不认识任何其他人。现在有一个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. html5 canvas绘制环形进度条,环形渐变色仪表图

    html5 canvas绘制环形进度条,环形渐变色仪表图                                             在绘制圆环前,我们需要知道canvas arc() 方 ...

  2. Log Files

    Description Nikolay has decided to become the best programmer in the world! Now he regularly takes p ...

  3. PSP1123

    PSP时间图: 类型 任务 开始时间 结束时间 净时间 中断时间 日期 开会 开会 16:17 16:50 33 0 20171027 开会 开会 17:00 17:22 22 0 20171028 ...

  4. 互评Alpha版本——Thunder团队

    基于NABCD评论作品 Hello World! :http://www.cnblogs.com/120626fj/p/7807544.html 欢迎来怼 :http://www.cnblogs.co ...

  5. c#数据库乱码

    1.sql连接语句加charset=utf8: 2.不要使用odbcConnection. 在由utf8改为latin1时候,需要修改的地方: 1.连接数据库语句中的charset: 2.在sql语句 ...

  6. 系统常量对话框QT实现

    1.运行结果: 2.代码 main.cpp #include "constantdiag.h" #include <QtWidgets/QApplication> in ...

  7. HashMap get()返回值问题

    问题描述:在进行mysql查询必要字段后,需要根据id进行es其它数据字段的查询拼接.使用HashMap以id为key 以查询过来的数据值为value. 代码如下: Map<String,Int ...

  8. 生成以指定字符为开头的md5值(6位数字)

    以下脚本的功能是生成以指定字符为开头的md5值 #-*- coding:utf-8 -*- #脚本功能:生成以指定字符为开头的md5值(6位数字) import hashlib import rand ...

  9. QT分析之QApplication的初始化

    原文地址:http://blog.163.com/net_worm/blog/static/1277024192010097430321/ 在开始分析之前交代一下,一是分析的QT在Window平台实现 ...

  10. [OS] 生产者-消费者问题(有限缓冲问题)

    ·最简单的情形--(一个生产者 + 一个消费者 + 一个大小为1的有限缓冲) 首先来分析其中的同步关系: ·必须在生产者放入一个产品之后,消费者才能够从缓冲中取出产品来消费.·只有在消费者从缓冲区中取 ...