關於集合的三道程式設計題
阿新 • • 發佈:2018-12-13
1.圖論某班有 40 個學生,學號為 180201-180240, 全部參加 Java 集合階段檢測,給出所有同學的成績 (可隨機產生,範圍為 50-100),請編寫程式將本班 各位同學成績從高往低排序列印輸出, 注:成績相同時學號較小的優先列印 要求:列印的資訊包含學號、姓名(姓名統一為 “同學 J”[J=10,11,12。。。])和成績 。
import java.util.*; /** * 描述:從大到小列印學生的:學號+姓名+成績 * * @Author administrator{GINO ZHANG} * @Date2018/11/22 */ public class JavaTest1 { private static List<Student> data = new ArrayList<Student>(); static class Student{ private int id; private String name; private int score; public Student(int id,String name,int score){ this.id = id; this.name = name; this.score = score; } public int getId() { return id; } public String getName() { return name; } public int getScore() { return score; } public String toString() { return "學號:" + id + ", 姓名:" + name + ", 成績:" + score ; } } public static void initData(){ int j = 10; Random random = new Random(); for (int i = 0;i < 40;++i){ data.add(new Student(180201+i,"同學"+(i+j),random.nextInt(51)+50)); } } public static void adjust(){ PriorityQueue<Student> queue = new PriorityQueue<Student>(11, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { if (o1.getScore() == o2.getScore()) { return o1.getId() - o2.getId(); } return o2.getScore() - o1.getScore(); } }); Iterator<Student> iterator1 = data.iterator(); while (iterator1.hasNext()){ queue.add(iterator1.next()); } for (int i = 0; i < 40; i++) { System.out.println(queue.remove()); } /**Iterator<Student> iterator2 = queue.iterator(); while (iterator2.hasNext()){ Student next1 = iterator2.next(); int id = next1.getId(); String name = next1.getName(); int score = next1.getScore(); System.out.println("學號:"+id+" 姓名:"+name+" 成績:"+score); }*/ } public static void print(){ Iterator<Student> iterator = data.iterator(); while (iterator.hasNext()){ Student next1 = iterator.next(); int id = next1.getId(); String name = next1.getName(); int score = next1.getScore(); System.out.println("學號:"+id+" 姓名:"+name+" 成績:"+score); } } public static void main(String[] args) { JavaTest javaTest = new JavaTest(); initData(); adjust(); } }
列印結果:
C:\java\java7\jdk1.7.0_80\bin\java.exe -javaagent:D:\ideaIU-2018.1.5.win\lib\idea_rt.jar=61408:D:\ideaIU-2018.1.5.win\bin - 學號:180209, 姓名:同學18, 成績:100 學號:180224, 姓名:同學33, 成績:100 學號:180238, 姓名:同學47, 成績:100 學號:180211, 姓名:同學20, 成績:98 學號:180212, 姓名:同學21, 成績:98 學號:180218, 姓名:同學27, 成績:96 學號:180237, 姓名:同學46, 成績:94 學號:180210, 姓名:同學19, 成績:92 學號:180206, 姓名:同學15, 成績:91 學號:180234, 姓名:同學43, 成績:91 學號:180240, 姓名:同學49, 成績:91 . . . Process finished with exit code 0
2.有兩個有序的集合,集合的每個元素都是一段範圍, 求其交集,例如交集{[4,8],[9,13]}和{[6,12]}的交集是 {[6,8],[9,12]}
/**
- @param collects1:集合 1
- @param collects2:集合 2
*/
public static void intersectionWithColletion (List<List> collects1, List<List> collects2) {
}
分析:
import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * 描述:就兩個集合的交集 * * @Author administrator{GINO ZHANG} * @Date2018/11/21 */ public class CollectsTest { private static boolean isVaild(List<List<Integer>> collects) { if (collects.size() < 1) { System.err.println("兩集合無交集"); return false; } Iterator<List <Integer>> iterator = collects.iterator(); while (iterator.hasNext()) { List <Integer> list = iterator.next(); if (list.size() != 2) { System.err.println("集合區間不合法"); return false; } if (list.get(0) > list.get(1)) { System.err.println("集合區間非有序"); return false; } } return true; } public static void intersectionWithColletion(List<List<Integer>> collects1, List<List<Integer>> collects2) { if (!(isVaild(collects1) && isVaild(collects2))) { return; } Iterator <List <Integer>> iterator1 = collects1.iterator(); while (iterator1.hasNext()) { List <Integer> list1 = iterator1.next(); Iterator <List <Integer>> iterator2 = collects2.iterator(); while (iterator2.hasNext()) { List <Integer> list2 = iterator2.next(); intersection(list1, list2); } } } private static void intersection(List<Integer> list1, List<Integer> list2) { Integer a1 = list1.get(0); Integer a2 = list1.get(1); Integer b1 = list2.get(0); Integer b2 = list2.get(1); if (a1 > b2 || b1 > a2) { return; } if (a1 == b2) { System.out.println("["+a1+"]"); } if (a2 == b1) { System.out.println("["+a2+"]"); } if (a1 >= b1) { System.out.print("[" + a1 + ","); if (b2 < a2) { System.out.println(b2 + "]"); } else { System.out.println(a2 + "]"); } } if (b1 >= a1) { System.out.print("["+b1+","); if (b2 < a2) { System.out.println(b2 + "]"); } else { System.out.println(a2 + "]"); } } } public static void main(String[] args) { ArrayList<List<Integer>> a = new ArrayList<List<Integer>>(); ArrayList<List<Integer>> b = new ArrayList<List<Integer>>(); ArrayList<Integer> arrayList1 = new ArrayList<Integer>(); arrayList1.add(4); arrayList1.add(8); ArrayList<Integer> arrayList2 = new ArrayList<Integer>(); arrayList2.add(9); arrayList2.add(13); ArrayList<Integer> arrayList3 = new ArrayList<Integer>(); arrayList3.add(6); arrayList3.add(12); a.add(arrayList1); a.add(arrayList2); b.add(arrayList3); intersectionWithColletion(a,b); } }
列印結果:
C:\java\java7\jdk1.7.0_80\bin\java.exe -javaagent:D:\ideaIU-2018.1.5.win\lib\idea_rt.jar=61449:D:\ideaIU-2018.1.5.win\bin -
[6,8]
[9,12]
Process finished with exit code 0
3.一個列表中記錄了 1000 條資料[資料內容需 自己初始化產生]。資料內容為部分使用者的銀行卡 消費記錄,消費記錄資訊包含([姓名] [消費金額]), 找出消費次數最多的使用者,並且該使用者單筆消費 最高的金額是多少?
例:列表內容格式
張三,666
李四,7893
張三,9999
獲取結果則為:張三消費次數最多,最高消費
金額為:9999
import java.util.*;
/**
* 描述:從隨機一千組資料中找出消費記錄最多的人,和他的最高的消費額
*
* @Author administrator{GINO ZHANG}
* @Date2018/11/21
*/
public class JavaTest3 {
static class Person {
private String name;
private int money;
public Person(String name, int money) {
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", money=" + money +
'}';
}
}
private static ArrayList<Person> arrayList = new ArrayList<>();
public static void initData(List<String> userList) {
Random random = new Random();
for (int i = 0; i < 1000; i++) {
arrayList.add(new Person(userList.get(random.nextInt(3)), random.nextInt(10000)));
}
}
public static void searchUser() {
HashMap<String, Integer> hashMap = new HashMap<>();
Iterator<Person> iterator = arrayList.iterator();
while (iterator.hasNext()) {
Person person = iterator.next();
if (hashMap.containsKey(person.getName())) {
int value = hashMap.get(person.getName());
hashMap.put(person.getName(), value + 1);
} else {
hashMap.put(person.getName(), 1);
}
}
PriorityQueue<Map.Entry<String, Integer>> priorityQueue = new PriorityQueue<>(10, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue() - o1.getValue();
}
});
Iterator<Map.Entry<String, Integer>> iterator1 = hashMap.entrySet().iterator();
while (iterator1.hasNext()) {
Map.Entry<String, Integer> value = iterator1.next();
priorityQueue.add(value);
}
//消費次數最多的 最大一筆的金額
int maxMoney = 0;
Iterator<Person> iterator2 = arrayList.iterator();
while (iterator2.hasNext()) {
Person person = iterator2.next();
String name = priorityQueue.peek().getKey();
Integer money = person.getMoney();
while (person.getName().equals(name)) {
if (person.getMoney() > maxMoney) {
maxMoney = person.getMoney();
} else {
break;
}
}
}
System.out.println(priorityQueue.peek().getKey() + " 為消費次數最多的人" + ",最高消費金額為 :" + maxMoney);
}
public static void print() {
Iterator<Person> iterator = arrayList.iterator();
while (iterator.hasNext()) {
Person person = iterator.next();
System.out.println(person + " ");
}
}
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("張三");
arrayList.add("李四");
arrayList.add("王五");
initData(arrayList);
print();
searchUser();
}
}
列印結果:
C:\java\java7\jdk1.7.0_80\bin\java.exe -javaagent:D:\ideaIU-2018.1.5.win\lib\idea_rt.jar=61476:D:\ideaIU-2018.1.5.win\bin -
Person{name='李四', money=8359}
Person{name='李四', money=1048}
Person{name='王五', money=3827}
Person{name='李四', money=2158}
Person{name='李四', money=553}
Person{name='王五', money=837}
Person{name='王五', money=2870}
Person{name='張三', money=9936}
.
.
.
張三 為消費次數最多的人,最高消費金額為 :9960