Spell more 2018/8/5 algorithm engineer written test

Source: Internet
Author: User

Given a list of friends with n<=100 users <=100, for a given user, find the person most likely to know about the user. The person most likely to know is defined as this person is not a friend of the current user (A is a friend of B, B is a friend), but has the most common friends. If the user and so people have no common friends to return-1.

Input:

5 0
1 2 3
4 0
4 0
4 0
1 2 3

Output:
4

C + + code

/*5 2 2 3*/#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>using namespacestd;intn,m;stringa[ the];intMain () {scanf ("%d%d", &n,&m);    GetChar ();  for(intI=0; i<n; i++) {getline (cin, A[i]); //cout<<a[i]<<endl;    }    intmaxs=-1; intmark=-1;  for(intk=0; k<n; k++)    {        if(k==m)Continue; intCount=0; intkey=0;  for(intI=0; I<a[m].length (); i+=2)        {            intval = a[m][i]- -;  for(intj=0; J<a[k].length (); j+=2)            {               intAA = A[val][j]- -; if(val==k&&aa==m) {key=1;  Break; }               //if (VAL==AA)                if(a[m][i]==A[k][j]) {Count++;  Break; }            }            if(Key)Continue; }        if(Key)Continue; if(count>maxs) {Maxs=count; Mark=K; }} printf ("%d\n", Mark); return 0;}

Java code:

import java.util.arraylist;import java.util.hashmap;import java.util.iterator;import Java.util.Map;import Java.util.map.entry;import Java.util.Scanner; Public classQuestionthree { Public Static voidMain (string[] args) {Scannerinch=NewScanner (System.inch); intn=inch. Nextint (); intk=inch. Nextint (); inch. nextline (); HashMap<integer, arraylist<integer>> inputmap=NewHashmap<>();  for(intI=0; i<n;i++) {String s=inch. nextline (); ArrayList<Integer> templist=NewArrayList ();  for(String Val:s.split (" ") ) Templist.add (Integer.parseint (Val)); if(i==k| |!Templist.contains (k)) Inputmap.put (i,templist); } System. out. println (Find (k, inputmap)); }         Public Static intFindintK, Hashmap<integer, arraylist<integer>>InputMap) {ArrayList<Integer> Klist=inputmap.Get(k); Iterator ITER=Inputmap.entryset (). iterator (); intindex=Integer.max_value; intmaxfriends=0;  while(Iter.hasnext ()) {Map.entry Entry=(Map.entry) iter.next (); ArrayList<Integer> templist= (arraylist<integer>) Entry.getvalue (); intCount=0; if((int) Entry.getkey () ==k)Continue;  for(intval:templist)if(Klist.contains (val)) Count++; if(count>maxfriends) {Maxfriends=count; Index=(int) Entry.getkey (); }            if(Maxfriends==count&&index> (int) Entry.getkey ()) Index=(int) Entry.getkey (); }                returnindex; }}

Spell more 2018/8/5 algorithm engineer written test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.