Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Adrijana on 7/21/2017.
- */
- import sun.reflect.generics.tree.Tree;
- import java.util.*;
- import java.util.stream.Collectors;
- 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
- class Movie implements Comparable<Movie> {
- public String title;
- public int[] ratings;
- public float average;
- public float coefWithoutMax;
- public Movie(String title, int[] ratings) {
- this.title = title;
- this.ratings = ratings;
- int sum = 0;
- for (int i = 0; i < ratings.length; ++i) {
- sum += ratings[i];
- }
- this.average = ((float) sum / (float) ratings.length);
- this.coefWithoutMax = (this.average * ratings.length);
- }
- @Override
- public String toString() {
- return String.format("%s (%.2f) of %d ratings", this.title, this.average, this.ratings.length);
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Movie movie = (Movie) o;
- if (Float.compare(movie.average, average) != 0) return false;
- if (Float.compare(movie.coefWithoutMax, coefWithoutMax) != 0) return false;
- if (title != null ? !title.equals(movie.title) : movie.title != null) return false;
- return Arrays.equals(ratings, movie.ratings);
- }
- @Override
- public int compareTo(Movie m) {
- if (Float.compare(this.average, m.average) != 0) {
- return Float.compare(this.average, m.average);
- }
- if(Float.compare(this.coefWithoutMax, m.coefWithoutMax) != 0){
- return Float.compare(this.coefWithoutMax, m.coefWithoutMax);
- }
- return this.title.compareTo(m.title);
- }
- }
- class MoviesList {
- public Set<Movie> movies;
- public MoviesList() {
- this.movies = new TreeSet<>() ;
- }
- public void addMovie(String title, int[] ratings) {
- Movie movie = new Movie(title, ratings);
- this.movies.add(movie);
- }
- public float ratingForAllMovies() {
- float allRatings = 0;
- for (Movie movie : movies) {
- allRatings += movie.ratings.length;
- }
- return allRatings;
- }
- public List<Movie> top10ByAvgRating() {
- List<Movie> tempList = new ArrayList<Movie>();
- tempList.addAll(movies);
- List<Movie> tenBest = new ArrayList<>(10);
- int counter = 0;
- while (counter < 10) {
- Movie bestMovie = tempList.get(0);
- for (Movie m : tempList) {
- if (m.average > bestMovie.average) {
- bestMovie = m;
- }
- }
- tenBest.add(bestMovie);
- tempList.remove(bestMovie);
- counter++;
- }
- return tenBest;
- }
- public List<Movie> top10ByRatingCoef() {
- //просечен ретјтинг на филмот x вкупно број на рејтинзи на филмот / максимален број на рејтинзи
- // (од сите филмови во листата)
- List<Movie> tempList = new ArrayList<Movie>();
- tempList.addAll(movies);
- List<Movie> tenBest = new ArrayList<>(10);
- float ratingForAllMovies = ratingForAllMovies();
- int counter = 0;
- while (counter < 10) {
- Movie bestMovie = tempList.get(0);
- for (Movie m : tempList) {
- if((m.coefWithoutMax / ratingForAllMovies) > (bestMovie.coefWithoutMax/ ratingForAllMovies)){
- bestMovie = m;
- }
- }
- tenBest.add(bestMovie);
- tempList.remove(bestMovie);
- counter++;
- }
- return tenBest;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement