编程题:sdut-Collection-sort--C~K的班级(II)
经过不懈的努力,C~K终于当上了班主任。
现在他要统计班里学生的名单,但是C~K在教务系统中导出班级名单时出了问题,发现会有同学的信息重复,现在他想把重复的同学信息删掉,只保留一个,
但是工作量太大了,所以找到了会编程的你,你能帮他解决这个问题吗?
### 输入格式:
第一行输入一个N,代表C~K导出的名单共有N行(N<100000).
接下来的N行,每一行包括一个同学的信息,学号 姓名 年龄 性别。
### 输出格式:
第一行输出一个n,代表删除重复名字后C~K的班级共有几人。
接下来的n行,输出每一个同学的信息,输出按照学号从小到大的顺序。
### 输入样例:
in
6
0001 MeiK 20 M
0001 MeiK 20 M
0002 sdk2 21 M
0002 sdk2 21 M
0002 sdk2 21 M
0000 blf2 22 F
### 输出样例:
out
3
0000 blf2 22 F
0001 MeiK 20 M
0002 sdk2 21 M
答案:若无答案欢迎评论
JAVA参考代码(1):List存储-->List排序(学号从小到大)-->对List进行输出
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
List<Student> stuList=new ArrayList<Student>();
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
Student stu=new Student(sc.next(),sc.next(),sc.nextInt(),sc.next().charAt(0));
if(!stuList.contains(stu))
{
stuList.add(stu);
}
}
Collections.sort(stuList);
System.out.println(stuList.size());
Iterator<Student> it2 = stuList.iterator();
while(it2.hasNext())
{
Student next = it2.next();
System.out.println(next);
}
sc.close();
}
}
class Student implements Comparable<Student>{
String id;
String name;
int age;
char sex;
public Student(String id, String name, int age, char sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + sex;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex != other.sex)
return false;
return true;
}
@Override
public String toString() {
return id + " " + name + " " + age + " " + sex ;
}
@Override
public int compareTo(Student o) {
return this.id.compareTo(o.id);
}
}
JAVA参考代码(2):Set存储-->Setl转为List-->List排序(学号从小到大)-->对List进行输出
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int n=reader.nextInt();
HashSet<Student2> stuSet=new HashSet<Student2>();
for(int i=0;i<n;i++)
{
Student2 stu=new Student2(reader.next(),reader.next(),reader.nextInt(),reader.next().charAt(0));
stuSet.add(stu);
}
List<Student2> stuList=new ArrayList(stuSet);
Collections.sort(stuList);
System.out.println(stuList.size());
for(Student2 stu1:stuList)
{
System.out.println(stu1);
}
reader.close();
}
}
class Student2 implements Comparable<Student2>{
String id;
String name;
int age;
char sex;
public Student2(String id, String name, int age, char sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return id + " " + name + " " + age + " " + sex ;
}
@Override
public int compareTo(Student2 o) {
return this.id.compareTo(o.id);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + sex;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student2 other = (Student2) obj;
if (age != other.age)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex != other.sex)
return false;
return true;
}
}
现在他要统计班里学生的名单,但是C~K在教务系统中导出班级名单时出了问题,发现会有同学的信息重复,现在他想把重复的同学信息删掉,只保留一个,
但是工作量太大了,所以找到了会编程的你,你能帮他解决这个问题吗?
### 输入格式:
第一行输入一个N,代表C~K导出的名单共有N行(N<100000).
接下来的N行,每一行包括一个同学的信息,学号 姓名 年龄 性别。
### 输出格式:
第一行输出一个n,代表删除重复名字后C~K的班级共有几人。
接下来的n行,输出每一个同学的信息,输出按照学号从小到大的顺序。
### 输入样例:
in
6
0001 MeiK 20 M
0001 MeiK 20 M
0002 sdk2 21 M
0002 sdk2 21 M
0002 sdk2 21 M
0000 blf2 22 F
### 输出样例:
out
3
0000 blf2 22 F
0001 MeiK 20 M
0002 sdk2 21 M
答案:若无答案欢迎评论
JAVA参考代码(1):List存储-->List排序(学号从小到大)-->对List进行输出
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
List<Student> stuList=new ArrayList<Student>();
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
Student stu=new Student(sc.next(),sc.next(),sc.nextInt(),sc.next().charAt(0));
if(!stuList.contains(stu))
{
stuList.add(stu);
}
}
Collections.sort(stuList);
System.out.println(stuList.size());
Iterator<Student> it2 = stuList.iterator();
while(it2.hasNext())
{
Student next = it2.next();
System.out.println(next);
}
sc.close();
}
}
class Student implements Comparable<Student>{
String id;
String name;
int age;
char sex;
public Student(String id, String name, int age, char sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + sex;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex != other.sex)
return false;
return true;
}
@Override
public String toString() {
return id + " " + name + " " + age + " " + sex ;
}
@Override
public int compareTo(Student o) {
return this.id.compareTo(o.id);
}
}
JAVA参考代码(2):Set存储-->Setl转为List-->List排序(学号从小到大)-->对List进行输出
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int n=reader.nextInt();
HashSet<Student2> stuSet=new HashSet<Student2>();
for(int i=0;i<n;i++)
{
Student2 stu=new Student2(reader.next(),reader.next(),reader.nextInt(),reader.next().charAt(0));
stuSet.add(stu);
}
List<Student2> stuList=new ArrayList(stuSet);
Collections.sort(stuList);
System.out.println(stuList.size());
for(Student2 stu1:stuList)
{
System.out.println(stu1);
}
reader.close();
}
}
class Student2 implements Comparable<Student2>{
String id;
String name;
int age;
char sex;
public Student2(String id, String name, int age, char sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return id + " " + name + " " + age + " " + sex ;
}
@Override
public int compareTo(Student2 o) {
return this.id.compareTo(o.id);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + sex;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student2 other = (Student2) obj;
if (age != other.age)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex != other.sex)
return false;
return true;
}
}