JavaRush /Java блог /Архив info.javarush /Кухня(); Задание N44
terranum
28 уровень
Milan

Кухня(); Задание N44

Статья из группы Архив info.javarush
Кухня(); Задание N44 - 1 Правила [Одномерные массивы] 44. Японская радиокомпания провела опрос N радиослушателей по вопросу: "Какое животное Вы связываете с Японией и японцами?" Составить программу получения k наиболее часто встречающихся ответов и их долей (в процентах). Кухня(); Задание N44 - 2
Комментарии (3)
ЧТОБЫ ПОСМОТРЕТЬ ВСЕ КОММЕНТАРИИ ИЛИ ОСТАВИТЬ КОММЕНТАРИЙ,
ПЕРЕЙДИТЕ В ПОЛНУЮ ВЕРСИЮ
Airon Уровень 34
15 сентября 2014
Я очень плохо знаком с compareTo, у меня он почему то не пашет. А вот уже сколько раз опробованный как часы.
public static void selectMaxAnimal(int k, String... array) {
    CountAnimal[] temp = new CountAnimal[]{new CountAnimal(array[0], 1)};
    for (int i = 1; i < array.length; i++)
        for (int j = 0; j < temp.length; j++)
            if(array[i].equals(temp[j].name)) {
                temp[j].count++;
                break;
            }
            else if(j == temp.length - 1){
                temp = Arrays.copyOf(temp, temp.length + 1);
                temp[temp.length - 1] = new CountAnimal(array[i], 0);
            }
    Arrays.sort(temp, new Comparator<CountAnimal>() {
        public int compare(CountAnimal o1, CountAnimal o2) {
            return o2.count - o1.count;
        }
    });
    k = k > temp.length ? temp.length : k;     // check k!
    for (int i = 0; i < k; i++)
        System.out.println(String.format("%d  %s  %d  %.3f%%", i + 1, temp[i].name, temp[i].count, temp[i].count*100.0/array.length));
}

public static class CountAnimal {
    public String name;
    public int count;

    public CountAnimal(String name, int count) {
        this.name = name;
        this.count = count;
    }    
}
Vash_the_Stampede Уровень 11
10 сентября 2014

    public static class Count implements Comparable<Count> {
        private String name;
        private int count;

        public Count(String name, int count) {
            this.name = name;
            this.count = count;
        }

        public int compareTo(Count c) {
            return count > c.count ? -1 : count < c.count ? 1 : 0;
        }
    }

    public static void solve(String[] arr, int k) {
        int n = arr.length;
        int namesCount = 0; // number of different names
        String[] names = new String[n];
        int[] counts = new int[n];

        for (int i = 0; i < n; i++) {
            boolean flag = true;
            for (int j = 0; j < namesCount; j++) {
                if (arr[i].equals(names[j])) {
                    counts[j]++;
                    flag = false;
                }
            }
            if (flag) {
                names[namesCount] = arr[i];
                counts[namesCount++] = 1;
            }
        }

        Count[] pairs = new Count[namesCount];
        for (int i = 0; i < namesCount; i++) {
            pairs[i] = new Count(names[i], counts[i]);
        }
        Arrays.sort(pairs);

        for (int i = 0; i < k; i++) {
            System.out.printf("%s %02.2f%%\n", pairs[i].name, pairs[i].count * 100.0 / n);
        }
    }