Advertisement
add1ctus

Најдобри филмови

Jan 26th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Movie
  4. {
  5.     private static int maxRatings;
  6.     private String title;
  7.     private int[] ratings;
  8.    
  9.     public Movie(String t, int[] r)
  10.     {
  11.         title = t;
  12.         ratings = r;
  13.         maxRatings = Math.max(maxRatings, ratings.length);
  14.     }
  15.    
  16.     private float avgRating()
  17.     {
  18.         int sum = 0;
  19.         for(int i = 0 ; i < ratings.length ; ++i)
  20.             sum += ratings[i];
  21.         return (float)sum/ratings.length;
  22.     }
  23.    
  24.     private float ratingCoef()
  25.     {
  26.         return avgRating() * ratings.length / maxRatings;
  27.     }
  28.    
  29.     public static class AvgRating implements Comparator<Movie>
  30.     {
  31.         public int compare(Movie m1, Movie m2)
  32.         {
  33.             float a1 = m1.avgRating();
  34.             float a2 = m2.avgRating();
  35.             if(a1 == a2)
  36.                 return m1.title.compareTo(m2.title);
  37.             if(a1 < a2)
  38.                 return 1;
  39.             return -1;
  40.         }
  41.     }
  42.    
  43.     public static class CoefRating implements Comparator<Movie>
  44.     {
  45.         public int compare(Movie m1, Movie m2)
  46.         {
  47.             float a1 = m1.ratingCoef();
  48.             float a2 = m2.ratingCoef();
  49.             if(a1 < a2)
  50.                 return 1;
  51.             if(a1 > a2)
  52.                 return -1;
  53.             return m1.title.compareTo(m2.title);
  54.         }
  55.     }
  56.    
  57.     public String toString()
  58.     {
  59.         return title + " (" + String.format("%.2f",avgRating()) + ") of " + ratings.length + " ratings";
  60.     }
  61.    
  62. }
  63.  
  64. class MoviesList
  65. {
  66.     private List<Movie> movies;
  67.    
  68.     public MoviesList()
  69.     {
  70.         movies = new ArrayList<Movie>();
  71.     }
  72.    
  73.     public void addMovie(String title, int[] ratings)
  74.     {
  75.         movies.add(new Movie(title, ratings));
  76.     }
  77.    
  78.     public List<Movie> top10ByAvgRating()
  79.     {
  80.         List<Movie> result = new ArrayList<Movie>();
  81.         for(int i = 0 ; i < movies.size() ; ++i)
  82.             result.add(movies.get(i));
  83.         Collections.sort(result,new Movie.AvgRating());
  84.         return result.subList(0, 10);
  85.     }
  86.    
  87.     public List<Movie> top10ByRatingCoef()
  88.     {
  89.         List<Movie> result = new ArrayList<Movie>();
  90.         for(int i = 0 ; i < movies.size() ; ++i)
  91.             result.add(movies.get(i));
  92.         Collections.sort(result,new Movie.CoefRating());
  93.         return result.subList(0, 10);
  94.     }
  95. }
  96.  
  97. public class MoviesTest {
  98.   public static void main(String[] args) {
  99.     Scanner scanner = new Scanner(System.in);
  100.     MoviesList moviesList = new MoviesList();
  101.     int n = scanner.nextInt();
  102.     scanner.nextLine();
  103.     for (int i = 0; i < n; ++i) {
  104.       String title = scanner.nextLine();
  105.       int x = scanner.nextInt();
  106.       int[] ratings = new int[x];
  107.       for (int j = 0; j < x; ++j) {
  108.         ratings[j] = scanner.nextInt();
  109.       }
  110.       scanner.nextLine();
  111.       moviesList.addMovie(title, ratings);
  112.     }
  113.     scanner.close();
  114.     List<Movie> movies = moviesList.top10ByAvgRating();
  115.     System.out.println("=== TOP 10 BY AVERAGE RATING ===");
  116.     for (Movie movie : movies) {
  117.       System.out.println(movie);
  118.     }
  119.     movies = moviesList.top10ByRatingCoef();
  120.     System.out.println("=== TOP 10 BY RATING COEFFICIENT ===");
  121.     for (Movie movie : movies) {
  122.       System.out.println(movie);
  123.     }
  124.   }
  125. }
  126.  
  127. // vashiot kod ovde
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement