天天看點

集合

一、函數

1、List中指定元素的删除

編寫以下兩個函數

1 /*以空格(單個或多個)為分隔符,将line中的元素抽取出來,放入一個List*/
2 public static List<String> convertStringToList(String line) 
3 /*在list中移除掉與str内容相同的元素*/
4 public static void remove(List<String> list, String str)      

裁判測試程式:

1 public class Main {
 2 
 3     /*covnertStringToList函數代碼*/   
 4 
 5     /*remove函數代碼*/
 6 
 7      public static void main(String[] args) {
 8         Scanner sc = new Scanner(System.in);
 9         while(sc.hasNextLine()){
10             List<String> list = convertStringToList(sc.nextLine());
11             System.out.println(list);
12             String word = sc.nextLine();
13             remove(list,word);
14             System.out.println(list);
15         }
16         sc.close();
17     }
18 
19 }      

樣例說明:底下展示了4組測試資料。

###輸入樣例

1 2 1 2 1 1 1 2
1
11 1 11 1 11
11
2 2 2 
1
1   2 3 4 1 3 1
1      

###輸出樣例

[1, 2, 1, 2, 1, 1, 1, 2]
[2, 2, 2]
[11, 1, 11, 1, 11]
[1, 1]
[2, 2, 2]
[2, 2, 2]
[1, 2, 3, 4, 1, 3, 1]
[2, 3, 4, 3]      
1 public static List<String> convertStringToList(String line) {
 2         List<String> a=new ArrayList<String>();
 3         String s[]=line.split("\\s+");
 4         for(int i=0;i<s.length;i++) {
 5             a.add(s[i]);
 6         }
 7         return a;
 8     }
 9     public static void remove(List<String> list, String str) {
10         for(int i=0;i<list.size();) {
11             if(list.get(i).equals(str)) {
12                 list.remove(i);
13                 i=0;
14             }else {
15                 i++;
16             }
17         }
18     }      

2、教師、學生排序

已知Main類、Person類的設計,完成Student類,Teacher類、MyTool類的設計。

函數接口定義:

class Student extends Person{ }
class Teacher extends Person{ }
class MyTool{ public static void separateStu_T(List persons,List teachers,List students){}   }      

Student類繼承了Person,擁有私有屬性int類型的sno和String類型的major,分别代表學号與所學專業;提供對應的set,get方法;比較方法完成按照學号比較。

Teacher類繼承了Person,擁有私有屬性int類型的tno和String類型的subject,分别代表教師編号與所授科目;提供對應的set,get方法;比較方法完成按年齡比較。

MyTool類中提供方法public static void separateStu_T(List persons,List teachers,List students){},方法 separateStu_T的功能是将persons線性表中的 teacher,student分别放到teachers,students兩個線性表中。

裁判測試程式樣例:

1 import java.util.*;
  2 
  3 public class Main {
  4     public static void main(String[] args) {
  5 
  6      List persons=getPersons();  //得到一個所有人的線性表
  7 
  8         List teachers=new ArrayList();
  9         List students=new ArrayList();
 10 
 11         MyTool.separateStu_T( persons,teachers,students); //将persons線性表中的 teacher,student分别放到teachers,students兩個線性表中
 12 
 13         Collections.sort(teachers);  //對教師線性表排序
 14         Collections.sort(students);  //對學生線性表排序
 15 
 16         showResult(teachers);  //顯示教師線性表排序以後的結果
 17         showResult(students);  //顯示學生線性表排序以後的結果
 18 
 19     }
 20 
 21     public static List getPersons()
 22     {
 23         List persons=new ArrayList();
 24 
 25         Scanner in=new Scanner(System.in);
 26         Person person=null;
 27 
 28         int num=Integer.parseInt(in.nextLine());
 29 
 30         for(int i=0;i<num;i++)
 31         {    String str=in.nextLine();
 32             String []data=str.split(",");
 33 
 34             if(data[0].equalsIgnoreCase("student"))
 35                  person=new Student(Integer.parseInt(data[1]),data[2],data[3],Integer.parseInt(data[4]),data[5]);
 36             else if (data[0].equalsIgnoreCase("teacher"))
 37                   person=new Teacher(Integer.parseInt(data[1]),data[2],data[3],Integer.parseInt(data[4]),data[5]);
 38             else person=null;
 39                  persons.add(person);
 40         }
 41         return persons;
 42     }
 43 
 44     public static void showResult(List persons)
 45     {
 46         for(int i=0;i<persons.size();i++)
 47         {
 48             Person per=(Person)persons.get(i);
 49             System.out.println(per.getName()+","+per.getGender()+","+per.getAge());
 50         }
 51     }
 52 
 53 
 54 }    
 55 
 56 
 57 
 58 abstract class Person  implements Comparable
 59 {    private String name;
 60      private String gender;
 61      private int age;
 62 
 63 
 64     public String getName() {
 65         return name;
 66     }
 67 
 68 
 69     public void setName(String name) {
 70         this.name = name;
 71     }
 72 
 73 
 74     public String getGender() {
 75         return gender;
 76     }
 77 
 78 
 79     public void setGender(String gender) {
 80         this.gender = gender;
 81     }
 82 
 83     public int getAge() {
 84         return age;
 85     }
 86 
 87     public void setAge(int age) {
 88         this.age = age;
 89     }
 90 
 91     public Person(String name, String gender, int age) {
 92         super();
 93         this.name = name;
 94         this.gender = gender;
 95         this.age = age;
 96     }
 97 
 98 
 99 }
100 
101 /* 請在這裡寫你的代碼 */      

輸入樣例:

輸入的第一行是總人數n,緊跟着輸入n行,每一行代表一個人的資訊。下面的例子中n=5,輸入了5個人的資訊。

5
student,1001,Tom1,female,18,computer
teacher,2001,Jake1,female,35,datastructer
student,1002,Tom2,male,19,computer
student,1003,Tom3,female,20,software
teacher,2002,Jake2,female,33,database      

輸出樣例:

注意:本樣例輸出結果中兩名教師是按照年齡升序排序的,三名學生是按照學号降序排序的。

Jake2,female,33
Jake1,female,35
Tom3,female,20
Tom2,male,19
Tom1,female,18      
class Student extends Person{
    private int sno;
    private String major;

    public Student(int sno, String name, String gender, int age, String major) {
        super(name, gender, age);
        this.sno = sno;
        this.major = major;
    }

    @Override
    public int compareTo(Object o) {
        return -(this.sno - ((Student)o).getSno());
    }

    public int getSno() {
        return sno;
    }

    public String getMajor() {
        return major;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public void setMajor(String major) {
        this.major = major;
    }
}

class Teacher extends Person{
    private int tno;
    private String subject;

    public Teacher(int tno, String name, String gender, int age, String subject) {
        super(name, gender, age);
        this.tno = tno;
        this.subject = subject;
    }

    @Override
    public int compareTo(Object o) {
        return this.getAge() - ((Teacher)o).getAge();
    }

    public int getTno() {
        return tno;
    }

    public String getSubject() {
        return subject;
    }

    public void setTno(int tno) {
        this.tno = tno;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }
}

class MyTool{
    public static void separateStu_T(List persons,List teachers,List students){
        for( Object o : persons ){
            if( o instanceof Student ){
                students.add(o);
            }
            else if( o instanceof Teacher ){
                teachers.add(o);
            }
        }
    }
}      

二、程式設計題

1、ArrayList入門

本習題主要用于練習如何使用ArrayList來替換數組。

建立1個​

​ArrayList<String> strList​

​用來存放字元串,然後進行如下操作。

**提示:**查詢Jdk文檔中的ArrayList。

**注意:**請使用​

​System.out.println(strList)​

​輸出清單元素。

輸入格式:

  1. 輸入n個字元串,放入​

    ​strList​

    ​。直到輸入為​

    ​!!end!!​

    ​時,結束輸入。
  2. 在​

    ​strList​

    ​頭部新增一個​

    ​begin​

    ​,尾部新增一個​

    ​end​

    ​。
  3. 輸出清單元素
  4. **輸入:**字元串​

    ​str​

  5. 判斷​

    ​strList​

    ​中有無包含字元串​

    ​str​

    ​,如包含輸出​

    ​true​

    ​,否則輸出​

    ​false​

    ​。并且輸出下标,沒包含傳回-1。
  6. 在strList中從後往前找。傳回其下标,找不到傳回-1。
  7. 移除掉第1個(下标為0)元素,并輸出。然後輸出清單元素。
  8. **輸入:**字元串str
  9. 将第2個(下标為1)元素設定為字元串str.
  10. 周遊strList,将字元串中包含str的元素放入另外一個​

    ​ArrayList strList1​

    ​,然後輸出strList1。
  11. 在strList中使用​

    ​remove​

    ​方法,移除第一個和str相等的元素。
  12. 輸出strList清單元素。
  13. 使用​

    ​clear​

    ​方法,清空strList。然後輸出strList的内容,​

    ​size()​

    ​與​

    ​isEmpty()​

    ​,3者之間用​

    ​,​

    ​連接配接。

輸入樣例:

a1 b1 3b a2 b2 12b c d !!end!!
b1
second
b      

輸出樣例:

[begin, a1, b1, 3b, a2, b2, 12b, c, d, end]
true
2
2
begin
[a1, b1, 3b, a2, b2, 12b, c, d, end]
[a1, second, 3b, a2, b2, 12b, c, d, end]
[3b, b2, 12b]
[a1, second, 3b, a2, b2, 12b, c, d, end]
[],0,true      
1 import java.util.ArrayList;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5         
 6     public static void main(String[] args){
 7         Scanner in = new Scanner(System.in);
 8         ArrayList<String> strList = new ArrayList<>();
 9         while(true)
10         {
11             String s = in.next();
12             if(s.equals("!!end!!"))
13                 break;
14             strList.add(s);
15         }
16         strList.add(0, "begin");
17         strList.add("end");
18         System.out.println(strList);
19         String str = in.next();
20         System.out.println(strList.contains(str));
21         System.out.println(strList.indexOf(str));
22         System.out.println(strList.lastIndexOf(str));
23         System.out.println(strList.get(0));
24         strList.remove(0);
25         System.out.println(strList);
26         str = in.next();
27         strList.set(1, str);
28         System.out.println(strList);
29         str = in.next();
30         ArrayList<String> strList1 = new ArrayList<>();
31         for(int i=0;i<strList.size();i++)
32         {
33             if(strList.get(i).contains(str))
34                 strList1.add(strList.get(i));
35         }
36         System.out.println(strList1);
37         strList.remove(str);
38         System.out.println(strList);
39         strList.clear();
40         System.out.println(strList+","+strList.size()+","+strList.isEmpty());
41     }
42 }      

2、字元串集合求并集 

從鍵盤接收N個英文字元串(其中不同的字元串數量大于10),從頭開始取5個不同的字元串放入一個集合S1,然後接着取5個不同的字元串放入另一個集合S2,按照字母順序輸出S1和S2的并集中的每個字元串(字元串區分大小寫)

輸入格式:

一行以空格分開的英文字元串(不同的字元串數量大于10)。

輸出格式:

按照字母順序(先比較字元串首字母,首字母相同的比較字元串第二個字母,以此類推)輸出的S1和S2并集的字元串。

android python java javaee javase database java jsp servlet java algorithm junit      
algorithm
android
database
java
javaee
javase
jsp
python
servlet      
1 import java.util.Iterator;
 2 import java.util.Scanner;
 3 import java.util.TreeSet;
 4 
 5 public class Main{
 6     public static void main(String[] args) {
 7         Scanner sc = new Scanner(System.in);
 8         String str =sc.nextLine();
 9         String[] s = str.split(" ");//分割字元串
10         TreeSet<String> s1 = new TreeSet<String>();
11         TreeSet<String> s2 = new TreeSet<String>();
12         int i = 0;
13         //取字元串放入TreeSet集合
14         while(s1.size() < 5 && i < s.length){
15             s1.add(s[i++]);
16         }
17         while(s2.size() < 5 && i < s.length){
18             s2.add(s[i++]);
19         }
20         //把兩個TreeSet集合放入新的TreeSet集合---》目的:自動排序所有字元串
21         TreeSet<String> s3 = new TreeSet<String>();
22         s3.addAll(s1);
23         s3.addAll(s2);
24         //輸出
25         Iterator it = s3.iterator();
26         while(it.hasNext()){
27             System.out.println(it.next());
28         }
29     }
30 }      

3、統計一段文字中的單詞個數并按單詞的字母順序排序後輸出 

現需要統計若幹段文字(英文)中的不同單詞數量。

如果不同的單詞數量不超過10個,則将所有單詞輸出(按字母順序),否則輸出前10個單詞。

注1:單詞之間以空格(1個或多個空格)為間隔。

注2:忽略空行或者空格行。

注3:單詞大小寫敏感,即'word'與'WORD'是兩個不同的單詞 。 ###輸入說明 若幹行英文,最後以​

​!!!!!​

​為結束。

###輸出說明 不同單詞數量。 然後輸出前10個單詞(按字母順序),如果所有單詞不超過10個,則将所有的單詞輸出。

Failure is probably the fortification in your pole
It is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta
when you Are wondering whether new money it has laid
background Because of you, then at the heart of the
most lax alert and most low awareness and left it
godsend failed
!!!!!      
49
Are
Because
Failure
It
a
alert
and
are
as
at      
1 import java.util.*;
 2  
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         Set<String> set = new TreeSet<String>();
 7         while(true) {
 8             String str;
 9             str = sc.next();
10             if(str.equals("!!!!!")) {
11                 break;
12             }
13             else {
14                 set.add(str);
15             }
16         }
17         System.out.println(set.size());
18         if(set.size() < 10) {
19             for(String str: set) {
20                 System.out.println(str);
21             }
22         }
23         else {
24             int count = 0;
25             for(String str: set) {
26                 if(count == 10) {
27                     break;
28                 }
29                 else {
30                     System.out.println(str);
31                     ++count;
32                 }
33             }
34         }
35         sc.close();
36     }
37 }      

4、學生成績讀取與排序

1)從鍵盤錄入多行學生成績的資訊,每行表示一個學生的一門課的成績,最後一行以“exit”結束。每行文本的格式為:學号,姓名,課程名,成績。程式能夠讀取學生及其成績,将具有相同學号的學生及其成績讀取到一個Student(學生類)類對象的清單(List)stuList中; 2)程式在讀取完學生及其成績的資料後,能夠将stuList中的學生按照平均成績降序排列(如果平均成績相同,學号數字小的排在前面), 并輸出排序後的學生學号、姓名和成績。

多行表示的學生成績,每一行是一個學生的姓名、學号、課程名、成績(整數)。不同行可以是同一名學生(學号相同的為同一名學生)不同課程的成績。

按照學生平均成績降序排序(平均成績相同的學号小的在前面)的學生排名(具體輸出格式參照樣例)。

小明,2001,Java,88
小剛,2002,Java,78
小丁,2003,Java,56
小宏,2004,Java,85
小明,2001,Python,84
小剛,2002,Python,98
小丁,2003,JavaWeb,66
小宏,2004,Algorithm,87
exit      
No1:2002,小剛
No2:2001,小明
No3:2004,小宏
No4:2003,小丁      
1 import java.util.*;
 2 class Student implements Comparable<Student>{
 3     private String name,id;
 4     private int subject=1;
 5     private int score;
 6     private double sum=0;
 7     Student(){}
 8     Student(String name,String id,int score){
 9              this.name=name;
10              this.id=id;
11              this.score=score;
12     }
13     public String getid(){
14         return this.id;
15     }
16     public void subjectadd() {
17         this.subject++;
18     }
19     public void scoreadd(int score){
20         this.score=this.score+score;
21     }
22     public String getname() {
23         return this.name;
24     }
25     public void sum() {
26         this.sum=this.score/this.subject;
27     }
28    /*public int rescore() {
29         return this.score;
30     }*/
31    /* public double getsum(){
32         return this.sum;
33     }*/
34     public int compareTo(Student o1){
35                Student one = (Student) o1;
36                if(this.sum-one.sum!=0)
37                return (int)(one.sum-this.sum);
38                else
39                return this.id.compareTo(one.id);
40     }
41 }
42 public class Main{
43     public static void main(String[] args) {
44             Scanner sc=new Scanner(System.in);
45             List<Student> list =new ArrayList<Student>();
46             int i,flag=0;
47             String k;
48             String[] and =new String[5];
49 
50             while(sc.hasNext())
51             {
52                 k=sc.next();
53                 and=k.split(",");
54                 if(k.compareTo("exit")==0)
55                 break;
56                 for(i=0;i<list.size();i++)
57                 {
58                     if(list.get(i).getid().compareTo(and[1])==0)
59                     {
60                         flag=1;
61                         break;
62                     }
63                 }
64                 if(flag==1)
65                 {
66                     list.get(i).subjectadd();
67                     list.get(i).scoreadd(Integer.parseInt(and[3]));
68                 }
69                 else
70                 {
71                     list.add(new Student(and[0],and[1],Integer.parseInt(and[3])));
72                 }
73                 flag=0;
74             }
75             for(i=0;i<list.size();i++)
76             list.get(i).sum();
77             Collections.sort(list);
78             for(i=0;i<list.size();i++)
79             {
80                 
81                 System.out.println("No"+(i+1)+":"+list.get(i).getid()+","+list.get(i).getname());
82                 //System.out.println(list.get(i).rescore());
83             }
84 
85     }
86 }      

5、多數組排序

3個整數數組進行整體排序,根據輸入的三個數組的元素,輸出排序後的結果(從大到小)

第1個數組的長度

第1個數組的各個元素

第2個數組的長度

第2個數組的各個元素

第3個數組的長度

第3個數組的各個元素

所有數組的整體排序

在這裡給出一組輸入。例如:

3 
79 80 61
3
88 66 77
2
23 90      

在這裡給出相應的輸出。例如:

90 88 80 79 77 66 61 23      
1 import java.util.Scanner;
 2 import java.util.Arrays;
 3 public class Main {
 4     public static void main(String []args)
 5     {
 6         Scanner s=new Scanner(System.in);
 7         int a=s.nextInt();
 8         int i;
 9         int []arr1=new int [a];
10         for(i=0;i<a;i++)
11         {
12             arr1[i]=s.nextInt();
13         }
14         int b=s.nextInt();
15         int []arr2=new int [b];
16         for(i=0;i<b;i++)
17         {
18             arr2[i]=s.nextInt();
19         }
20         int c=s.nextInt();
21         int []arr3=new int [c];
22         for(i=0;i<c;i++)
23         {
24             arr3[i]=s.nextInt();
25         }
26         int [] ee = new int[a + b + c];
27         System.arraycopy(arr1, 0, ee, 0, a);
28         System.arraycopy(arr2, 0, ee, a, b);
29         System.arraycopy(arr3, 0, ee, a + b, c);
30         Arrays.sort(ee);
31         System.out.print(ee[ee.length-1]);
32         for(i=ee.length-2;i>=0;i--)
33         {
34             System.out.print(" " + ee[i]);
35         }
36         s.close();
37     }
38 }      

6、讀中國載人航天史,彙航天員數量,向航天員緻敬 

1986年,中國實施“863”計劃,航天技術列入其中。以載人飛船開始起步,最終建成我國的空間站。 1992年9月21日,中國實施載人航天工程,并确定了三步走的發展戰略:第一步,發射載人飛船,建成初步配套的試驗性載人飛船工程。第二步,突破載人飛船和空間飛行器的交會對接技術,利用載人飛船技術改裝、發射一個空間實驗室。第三步,建造載人空間站。

在長期的奮鬥中,我國航天工作者不僅創造了非凡的業績,而且鑄就了特别能吃苦、特别能戰鬥、特别能攻關、特别能奉獻的載人航天精神。載人航天精神,是“兩彈一星”精神在新時期的發揚光大,是我們偉大民族精神的生動展現,永遠值得全黨、全軍和全國人民學習。

截至2021年4月,曆任航天英雄名字如下:
楊利偉(神舟五号)
費俊龍、聶海勝(神舟六号)
翟志剛、景海鵬、劉伯明(神舟七号)
景海鵬、劉旺、劉洋(神舟九号)
聶海勝、張曉光、王亞平(神舟十号)
景海鵬、陳東(神舟十一号)      

會程式設計的小夥伴們,請以他們姓名中的拼音字母排序,統計一下航天英雄們出征太空的次數,以實際行動向航天員們緻敬!

每次航天飛船的編号為一行讀入資料,分别讀入每次飛上太空的航天英雄的姓名,名字中間有若幹個空格分隔。
最後一行為“end“,表示輸入結束。      

以他們姓名中的拼音字母排序,統計航天英雄們出征太空的次數。 每位航天員占一行,航天員姓名與出征次數中間有一個空格。

YangLiWei楊利偉
FeiJunLong費俊龍    NieHaiSheng聶海勝
Zhaizhigang翟志剛   JingHaiPeng景海鵬           LiuBoMing劉伯明
JingHaiPeng景海鵬   LiuWang劉旺                 LiuYang劉洋
NieHaiSheng聶海勝   Zhangxiaoguang張曉光        WangYaPing王亞平
JingHaiPeng景海鵬   ChenDong陳東
end      
ChenDong陳東 1
FeiJunLong費俊龍 1
JingHaiPeng景海鵬 3
LiuBoMing劉伯明 1
LiuWang劉旺 1
LiuYang劉洋 1
NieHaiSheng聶海勝 2
WangYaPing王亞平 1
YangLiWei楊利偉 1
Zhaizhigang翟志剛 1
Zhangxiaoguang張曉光 1      
1 import java.util.*;
 2 import java.util.Map.Entry;
 3 
 4 
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         Scanner sc = new Scanner(System.in);
11        Map<String,Integer> mp=new TreeMap<String,Integer>();
12        String s=sc.next();
13        while(s.charAt(0)!='e'&&s.charAt(0)!='n'&&s.charAt(0)!='d') {
14            //System.out.println(s.charAt(0));
15            if(mp.containsKey(s)) {
16                mp.put(s, mp.get(s)+1);              
17            }else {                
18                mp.put(s, 1);
19            }
20            s=sc.next();
21 
22        }
23       
24        
25        
26        /*啊啊啊啊我這個憨批為什麼會看成按次數排序啊啊啊啊
27        List<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(mp.entrySet());
28 
29        Collections.sort(list,new Comparator<Map.Entry<String,Integer>>()
30        {
31 
32            @Override
33            public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
34             // TODO Auto-generated method stub
35              return o1.getValue().compareTo(o2.getValue());
36            }
37         
38        }   );
39 
40        for(Map.Entry<String,Integer> mapping:list){ 
41 
42            System.out.println(mapping.getKey()+" "+mapping.getValue()); 
43 
44       } */
45 
46 
47         Iterator it=mp.entrySet().iterator();
48         while(it.hasNext()) {
49             Entry ex =(Entry)it.next();
50             System.out.println(ex.getKey()+" "+ex.getValue()); 
51         }
52         
53     }
54 
55 }