Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Movie
- {
- private static int maxRatings;
- private String title;
- private int[] ratings;
- public Movie(String t, int[] r)
- {
- title = t;
- ratings = r;
- maxRatings = Math.max(maxRatings, ratings.length);
- }
- private float avgRating()
- {
- int sum = 0;
- for(int i = 0 ; i < ratings.length ; ++i)
- sum += ratings[i];
- return (float)sum/ratings.length;
- }
- private float ratingCoef()
- {
- return avgRating() * ratings.length / maxRatings;
- }
- public static class AvgRating implements Comparator<Movie>
- {
- public int compare(Movie m1, Movie m2)
- {
- float a1 = m1.avgRating();
- float a2 = m2.avgRating();
- if(a1 == a2)
- return m1.title.compareTo(m2.title);
- if(a1 < a2)
- return 1;
- return -1;
- }
- }
- public static class CoefRating implements Comparator<Movie>
- {
- public int compare(Movie m1, Movie m2)
- {
- float a1 = m1.ratingCoef();
- float a2 = m2.ratingCoef();
- if(a1 < a2)
- return 1;
- if(a1 > a2)
- return -1;
- return m1.title.compareTo(m2.title);
- }
- }
- public String toString()
- {
- return title + " (" + String.format("%.2f",avgRating()) + ") of " + ratings.length + " ratings";
- }
- }
- class MoviesList
- {
- private List<Movie> movies;
- public MoviesList()
- {
- movies = new ArrayList<Movie>();
- }
- public void addMovie(String title, int[] ratings)
- {
- movies.add(new Movie(title, ratings));
- }
- public List<Movie> top10ByAvgRating()
- {
- List<Movie> result = new ArrayList<Movie>();
- for(int i = 0 ; i < movies.size() ; ++i)
- result.add(movies.get(i));
- Collections.sort(result,new Movie.AvgRating());
- return result.subList(0, 10);
- }
- public List<Movie> top10ByRatingCoef()
- {
- List<Movie> result = new ArrayList<Movie>();
- for(int i = 0 ; i < movies.size() ; ++i)
- result.add(movies.get(i));
- Collections.sort(result,new Movie.CoefRating());
- return result.subList(0, 10);
- }
- }
- public class MoviesTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- MoviesList moviesList = new MoviesList();
- int n = scanner.nextInt();
- scanner.nextLine();
- for (int i = 0; i < n; ++i) {
- String title = scanner.nextLine();
- int x = scanner.nextInt();
- int[] ratings = new int[x];
- for (int j = 0; j < x; ++j) {
- ratings[j] = scanner.nextInt();
- }
- scanner.nextLine();
- moviesList.addMovie(title, ratings);
- }
- scanner.close();
- List<Movie> movies = moviesList.top10ByAvgRating();
- System.out.println("=== TOP 10 BY AVERAGE RATING ===");
- for (Movie movie : movies) {
- System.out.println(movie);
- }
- movies = moviesList.top10ByRatingCoef();
- System.out.println("=== TOP 10 BY RATING COEFFICIENT ===");
- for (Movie movie : movies) {
- System.out.println(movie);
- }
- }
- }
- // vashiot kod ovde
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement