Java 【 ArrayList應用 】 (SDUT 4069 C~K的班級)
阿新 • • 發佈:2018-12-01
Java 裡面的所有的東西 陣列、字元陣列、等等,都要 new 新申請。
C~K的班級
程式碼:
package test; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner (System.in); int n; ArrayList<String> a = new ArrayList<String>(); n = sc.nextInt(); sc.nextLine(); while(n -- > 0) { String s = sc.nextLine(); if(!a.contains(s)) { a.add(s); } else continue; } System.out.println(a.size()); for(int i = 0; i < a.size(); i ++) { System.out.println(a.get(i)); } } }
Time Limit: 1500 ms Memory Limit: 65536 KiB
Problem Description
經過不懈的努力,C~K終於當上了班主任。
現在他要統計班裡學生的名單,但是C~K在教務系統中匯出班級名單時出了問題,發現會有同學的資訊重複,現在他想把重複的同學資訊刪掉,只保留一個,
但是工作量太大了,所以找到了會程式設計的你,你能幫他解決這個問題嗎?Input
第一行輸入一個N,代表C~K匯出的名單共有N行(N<100000).
接下來的N行,每一行包括一個同學的資訊,學號 姓名 年齡 性別。Output
第一行輸出一個n,代表刪除重複名字後C~K的班級共有幾人。
接下來的n行,輸出每一個同學的資訊,輸出按照輸入的順序。Sample Input
6 0001 MeiK 20 M 0001 MeiK 20 M 0002 sdk2 21 M 0002 sdk2 21 M 0002 sdk2 21 M 0000 blf2 22 FSample Output
3 0001 MeiK 20 M 0002 sdk2 21 M 0000 blf2 22 F