• find the guy every one know


    There are N people in the town (N>=2), and function Know(i,j) return true if i knows j and false otherwise. The knowing relationship is not symmetric. Know(i,j)==true cannot guarantee Know(j,i)==true. Find those people: everyone knows him but he knows nobody else.
     

    int find()
    {
    int i =0;
    int j =1;
    int next =2;
    //first find the gay
    while(next <= N) //if still have someone not remove
    {
    if (Know(i, j)) //if i know j, i is not we want
    {
    i
    = next;
    }
    else
    {
    j
    = next;
    }
    ++next; //consider the next one
    }
    int cand = i; //candidate
    if (i == N)
    {
    cand
    = j;
    }
    //check if the cadidate is the one we want to find
    for (int k =0; k < N; ++k)
    {
    if (k == cond)
    {
    continue;
    }
    if (Know(cand, k) ||!Know(k, cand)) //if candidate know k or k not know candidate
    {
    return-1;
    }
    }
    return cand;
    }
  • 相关阅读:
    javaweb地图定位demo
    java基础循环
    java实现时钟
    栈和队列
    线程池
    java死锁及解决方案
    克隆
    算法与数据结构基础一
    重定向与转发的区别
    省选模拟57
  • 原文地址:https://www.cnblogs.com/triStoneL/p/1950931.html
Copyright © 2020-2023  润新知