zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    log4j
    hashContext
    sql语句
    css样式
    作业七:(二)疑问解答
    作业七:(1)翻译
    结对编程作业
    软件优缺点评价(补交)
    C#程序分析
    VS2013安装及测试练习
  • 原文地址:https://www.cnblogs.com/triStoneL/p/1950931.html
Copyright © 2011-2022 走看看