《Java语言程序设计与数据结构》编程练习答案(第二十章)(二)
英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition
20.9
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Comparator<Circle> circleComparator = new Comparator<Circle>() {
@Override
public int compare(Circle o1, Circle o2) {
return (int)(o2.getRadius()-o1.getRadius());
}
};
PriorityQueue<Circle> test = new PriorityQueue<>(1, circleComparator);
for(int i=0;i<10;i++){
test.add(new Circle(20*Math.random()));
}
for(int i=0;i<10;i++){
System.out.println(test.remove().getRadius());
}
}
}
class Circle{
private double radius;
private double square;
public Circle(double r){
this.radius = r;
this.square = Math.PI*r*r;
}
public double getRadius(){
return this.radius;
}
public double getSquare(){
return this.square;
}
}
20.10
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
PriorityQueue<String> set1 = new PriorityQueue<>();
PriorityQueue<String> set2 = new PriorityQueue<>();
set1.offer("George");
set1.offer("Jim");
set1.offer("John");
set1.offer("Blake");
set1.offer("Kevin");
set1.offer("Michael");
set2.offer("George");
set2.offer("Katie");
set2.offer("Kevin");
set2.offer("Michelle");
set2.offer("Ryan");
PriorityQueue<String> union = new PriorityQueue<>(set1);
for( String str: set2){
if(!union.contains(str)){
union.offer(str);
}
}
PriorityQueue<String> intersect = new PriorityQueue<>();
for(String str : set1){
if(set2.contains(str)){
intersect.offer(str);
}
}
PriorityQueue<String> diff = new PriorityQueue<>();
for(String str: set1){
if(!set2.contains(str)){
diff.offer(str);
}
}
System.out.println(Arrays.toString(union.toArray()));
System.out.println(Arrays.toString(intersect.toArray()));
System.out.println(Arrays.toString(diff.toArray()));
}
}
20.11
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the code: ");
String code = input.nextLine();
boolean isPair = true;
Stack<Character> test = new Stack<>();
for(int i=0;i<code.length();i++){
char tmp = code.charAt(i);
if(tmp == '(' || tmp == '[' || tmp == '{'){
test.push(tmp);
}
else if(tmp == ')'){
char left = test.pop();
if(left != '('){
System.out.println("Wrong");
isPair = false;
break;
}
}
else if(tmp == ']'){
char left = test.pop();
if(left != '['){
System.out.println("Wrong");
isPair = false;
break;
}
}
else if(tmp == '}'){
char left = test.pop();
if(left != '{'){
System.out.println("Wrong");
isPair = false;
break;
}
}
}
if(isPair){
if(test.isEmpty()){
System.out.println("Correct");
}
else{
System.out.println("Wrong");
}
}
}
}
20.12
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MyPriorityQueue<String> test = new MyPriorityQueue<>();
test.offer("114");
test.offer("514");
MyPriorityQueue<String> test1 = (MyPriorityQueue<String>) test.clone();
test1.offer("1919810");
System.out.println(Arrays.toString(test.toArray()));
System.out.println(Arrays.toString(test1.toArray()));
}
}
class MyPriorityQueue<T> extends PriorityQueue<T> implements Cloneable{
@Override
protected Object clone(){
MyPriorityQueue<T> myPriorityQueue = null;
try{
myPriorityQueue = (MyPriorityQueue<T>) super.clone();
}catch (CloneNotSupportedException e){
e.printStackTrace();
}
return myPriorityQueue;
}
}
20.13
🐎
20.14
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Stack<Character> test = new Stack<>();
System.out.println("Please enter the RPN");
String rpn = input.nextLine();
rpn = rpn.replace(" ","");
for(int i=0;i<rpn.length();i++){
char tmp = rpn.charAt(i);
if(tmp >= '0' && tmp <='9'){
test.push((char)(tmp-'0'));
}
else{
int op1 = (int)test.pop();
int op2 = (int)test.pop();
int res;
switch (tmp){
case '+':
res = op1 + op2;
test.push((char)res);
break;
case '-':
res = op1 - op2;
test.push((char)res);
break;
case '*':
res = op1 * op2;
test.push((char)res);
break;
case '/':
res = op1 / op2;
test.push((char)res);
break;
default:
break;
}
}
}
System.out.println("The result is "+(int)(test.pop()));
}
}
20.15
🐔
20.16
参见https://blog.csdn.net/swy_swy_swy/article/details/102713371
20.17
👨🐰🌶
20.18
20.21
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] list2 = {"red","blue","green","yellow","orange","pink"};
Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.charAt(o1.length()-1)-o2.charAt(o2.length()-1);
}
};
selectionSort(list2, comparator);
System.out.println(Arrays.toString(list2));
}
public static <T> void selectionSort(T[] list, Comparator< ? super T> comparator){
for(int i=0;i<list.length;i++){
int targetIndex = i;
int minIndex = i;
for(int j=i;j<list.length;j++){
if(comparator.compare(list[j],list[minIndex]) < 0){
minIndex = j;
}
}
T tmp = list[targetIndex];
list[targetIndex] = list[minIndex];
list[minIndex] = tmp;
}
}
}