实现一个功能,以返回电影网络中收视率最高的电影

时间:2015-08-07 19:48:38

标签: c# binary-tree

鉴于以下课程:

public class Movie
{
    private readonly char movieId;
    private readonly float rating;
    private List<Movie> similarMovies; // Similarity is bidirectional

    public Movie(char movieId, float rating)
    {
        this.movieId = movieId;
        this.rating = rating;
        similarMovies = new List<Movie>();
    }

    public char getId()
    {
        return movieId;
    }

    public float getRating()
    {
        return rating;
    }

    public void addSimilarMovie(Movie movie)
    {
        similarMovies.Add(movie);
        movie.similarMovies.Add(this);
    }

    public List<Movie> getSimilarMovies()
    {
        return similarMovies;
    }     

    public static IList<Movie> getMovieRecommendations(Movie movie, int numTopRatedSimilarMovies)
    {
        //Implement this method         
        return null;
    }
}

reachable from the current movie
         eg:            A(Rating 1.2)
                        /   \
                     B(2.4)  C(3.6)
                       \     /
                        D(4.8)
          在上面的例子中,边表示相似度,数字是评级。           getMovieRecommendations(A,2)应该返回C和D(排序顺序并不重要,因此它也可以返回D和C)           getMovieRecommendations(A,4)应该返回A,B,C,D(它也可以以任何顺序返回这些,例如:B,C,D,A)           getMovieRecommendations(A,1)应该返回D.注意从A到D的距离并不重要

1 个答案:

答案 0 :(得分:0)

 public static HashSet<Movie> sd = new HashSet<Movie>();
    public static IList<Movie> getMovieRecommendations(Movie movie, int numTopRatedSimilarMovies)
    {
        sd.Add(movie);
        foreach (Movie m in movie.getSimilarMovies())
        {
            if (!sd.Contains(m))
                getMovieRecommendations(m, -1);
        }
        if(numTopRatedSimilarMovies > -1)
            return sd.OrderByDescending(o => o.rating).Take(numTopRatedSimilarMovies).ToList();            
        return null;
    }