实现一个函数返回电影网络中最受好评的电影
本文关键字:电影 网络 电影网 受好评 返回 函数 一个 实现 | 更新日期: 2023-09-27 18:07:06
给定类:
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;
}
}
从当前电影可达A(等级1.2)/'B C (2.4) (3.6)'/D(4.8) 之前在上面的例子中,边表示相似性,数字表示评级。getmovierrecommendations (A,2)应该返回C和D(排序顺序无关,因此它也可以返回D和C)getmovierrecommendations (A,4)应该返回A, B,C,D(它也可以以任何顺序返回这些,例如:B,C,D,A)getmovierrecommendations (A,1)应该返回D。注意从A到D的距离不重要
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;
}