Created
April 12, 2020 11:40
-
-
Save ababup1192/f55cf825a3cad680bac10e743869b222 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.abab; | |
import java.util.Objects; | |
public class GoodsPair { | |
public final String name1; | |
public final String name2; | |
public GoodsPair(String name1, String name2) { | |
this.name1 = name1; | |
this.name2 = name2; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
GoodsPair goodsPair = (GoodsPair) o; | |
return Objects.equals(name1, goodsPair.name1) && | |
Objects.equals(name2, goodsPair.name2); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(name1, name2); | |
} | |
@Override | |
public String toString() { | |
return name1 + " " + name2; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.abab; | |
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int n = scanner.nextInt(); | |
// 配列は安全ではない→リストを使う。List<String> ListはジェネリックスでString型に。 | |
// ArrayListっていう配列のようなリストに。で、左でStringを記述してるから右は省略可能。(ダイヤモンドリスト) | |
List<GoodsPair> tmpGoodsPairList = new ArrayList<>(); | |
/* 購入者の入力ループ */ | |
for (int i = 0; i < n; i++) { | |
int m = scanner.nextInt(); | |
List<String> pList = new ArrayList<>(); | |
/* 商品の入力ループ */ | |
for (int j = 0; j < m; j++) { | |
pList.add(scanner.next()); | |
} | |
for (int j = 0; j < m - 1; j++) { | |
for (int k = j + 1; k < m; k++) { | |
String name1 = pList.get(j).compareTo(pList.get(k)) < 0 ? pList.get(j) : pList.get(k); | |
String name2 = pList.get(j).compareTo(pList.get(k)) > 0 ? pList.get(j) : pList.get(k); | |
tmpGoodsPairList.add(new GoodsPair(name1, name2)); | |
} | |
} | |
} | |
Map<GoodsPair, Integer> map = new HashMap<>(); | |
for (GoodsPair goodsPair : tmpGoodsPairList) { | |
if (map.containsKey(goodsPair)) { | |
map.put(goodsPair, map.get(goodsPair) + 1); | |
} else { | |
map.put(goodsPair, 1); | |
} | |
} | |
Set<Integer> quantitySet = new LinkedHashSet<>(map.values()); | |
List<Integer> quantities = new ArrayList<>(quantitySet); | |
quantities.sort(Comparator.comparingInt((i) -> -i)); | |
for (Integer q : quantities) { | |
List<GoodsPair> goodsPairList = new ArrayList<>(); | |
map.forEach((goodsPair, q2) -> { | |
if (q.equals(q2)) { | |
goodsPairList.add(goodsPair); | |
} | |
}); | |
goodsPairList.sort((gp1, gp2) -> { | |
if (gp1.name1.compareTo(gp2.name1) != 0) { | |
return gp1.name1.compareTo(gp2.name1); | |
} else { | |
return gp1.name2.compareTo(gp2.name2); | |
} | |
}); | |
for (GoodsPair goodsPair : goodsPairList) { | |
System.out.println(q + " " + goodsPair); | |
} | |
System.out.println("------"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment