Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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{
- String name;
- List<Integer> list;
- static int max;
- public Movie(String name, int [] rtg) {
- this.name = name;
- this.list = new ArrayList<>();
- for(Integer pom: rtg) {
- list.add(pom);
- }
- }
- public String getName() {
- return name;
- }
- public List<Integer> getList() {
- return list;
- }
- public double averageRt() {
- double sum=0;
- sum=list.stream().mapToInt(o -> o).sum();
- return sum/list.size();
- }
- public double averageCoefRt() {
- //double sum=0;
- //sum=list.stream().mapToInt(o -> o).sum();
- return averageRt() * list.size() / max;
- }
- public static void setMax(int max) {
- Movie.max = max;
- }
- @Override
- public String toString() {
- return String.format("%s (%.2f) of %d ratings", name,(float)averageRt(),list.size());
- }
- }
- class MoviesList{
- List<Movie> filmovi;
- int max=0;
- public MoviesList() {
- filmovi=new ArrayList<>();
- }
- public void addMovie(String title, int[] ratings) {
- filmovi.add(new Movie(title,ratings));
- if(max<ratings.length) {
- max=ratings.length;
- }
- }
- public List<Movie> top10ByAvgRating(){
- return filmovi.stream()
- .sorted(Comparator.comparing(Movie::averageRt).reversed().thenComparing(Movie::getName))
- .collect(Collectors.toList()).subList(0, 10);
- }
- public List<Movie> top10ByRatingCoef(){
- filmovi.get(0).setMax(max);
- return filmovi.stream().sorted(Comparator
- .comparing(Movie::averageCoefRt).reversed().thenComparing(Movie::getName))
- .collect(Collectors.toList()).subList(0, 10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment