概述
6-1 教师、学生排序 (30 分)
已知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两个线性表中。
裁判测试程序样例:
import java.util.*;
public class Main {
public static void main(String[] args) {
List persons=getPersons(); //得到一个所有人的线性表
List teachers=new ArrayList();
List students=new ArrayList();
MyTool.separateStu_T( persons,teachers,students); //将persons线性表中的 teacher,student分别放到teachers,students两个线性表中
Collections.sort(teachers); //对教师线性表排序
Collections.sort(students); //对学生线性表排序
showResult(teachers); //显示教师线性表排序以后的结果
showResult(students); //显示学生线性表排序以后的结果
}
public static List getPersons()
{
List persons=new ArrayList();
Scanner in=new Scanner(System.in);
Person person=null;
int num=Integer.parseInt(in.nextLine());
for(int i=0;i<num;i++)
{ String str=in.nextLine();
String []data=str.split(",");
if(data[0].equalsIgnoreCase("student"))
person=new Student(Integer.parseInt(data[1]),data[2],data[3],Integer.parseInt(data[4]),data[5]);
else if (data[0].equalsIgnoreCase("teacher"))
person=new Teacher(Integer.parseInt(data[1]),data[2],data[3],Integer.parseInt(data[4]),data[5]);
else person=null;
persons.add(person);
}
return persons;
}
public static void showResult(List persons)
{
for(int i=0;i<persons.size();i++)
{
Person per=(Person)persons.get(i);
System.out.println(per.getName()+","+per.getGender()+","+per.getAge());
}
}
}
abstract class Person implements Comparable
{ private String name;
private String gender;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, String gender, int age) {
super();
this.name = name;
this.gender = gender;
this.age = age;
}
}
/* 请在这里写你的代码 */
输入样例:
输入的第一行是总人数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
解析:
类的equals方法,comparable接口的实现,以及判断对象是那种类的方法。
AC代码:
class Student extends Person {
private int son;
private String major;
public Student(int son, String name, String gender, int age, String major) {
super(name, gender, age);
this.son = son;
this.major = major;
}
public int getSon() {
return son;
}
public void setSon(int son) {
this.son = son;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public int compareTo(Object o) {
Student student = (Student) o;
if(this.getSon() > student.getSon()) {
return -1;
} else if(this.getSon() < student.getSon()){
return 1;
}
return 0;
}
}
class Teacher extends Person {
private int ton;
private String subject;
public Teacher(int ton, String name, String gender, int age, String subject) {
super(name, gender, age);
this.ton = ton;
this.subject = subject;
}
public int getTon() {
return ton;
}
public void setTon(int ton) {
this.ton = ton;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public int compareTo(Object o) {
Teacher teacher = (Teacher) o;
if(this.getAge() < teacher.getAge()) {
return -1;
} else if(this.getAge() > teacher.getAge()) {
return 1;
}
return 0;
}
}
class MyTool {
public static void separateStu_T(List persons, List teachers, List students) {
for(int i = 0; i < persons.size(); i++) {
Object object = persons.get(i);
if(object instanceof Teacher) {
teachers.add(object);
} else if(object instanceof Student) {
students.add(object);
}
}
}
}
7-1 jmu-Java&Python-统计文字中的单词数量并按出现次数排序 (45 分)
现在需要统计若干段文字(英文)中的单词数量,并且还需统计每个单词出现的次数。
注1:单词之间以空格(1个或多个空格)为间隔。
注2:忽略空行或者空格行。
基本版:
统计时,区分字母大小写,且不删除指定标点符号。
进阶版:
- 统计前,需要从文字中删除指定标点符号
!.,:*?
。 注意:所谓的删除,就是用1个空格替换掉相应字符。 - 统计单词时需要忽略单词的大小写。
输入说明
若干行英文,最后以!!!!!
为结束。
输出说明
单词数量
出现次数排名前10的单词(次数按照降序排序,如果次数相同,则按照键值的字母升序排序)及出现次数。
输入样例1
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
!!!!!
输出样例1
46
the=4
it=3
you=3
and=2
are=2
is=2
most=2
of=2
when=2
your=2
输入样例2
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
Tom say: Who is the best? No one dare to say yes.
most lax alert and! most low awareness and* left it
godsend failed
!!!!!
输出样例2
54
the=5
is=3
it=3
you=3
and=2
are=2
most=2
of=2
say=2
to=2
解析:
字符串的处理,就是考察对容器以及String当中的方法的运用。
AC代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import javax.print.attribute.Size2DSyntax;
import javax.sound.sampled.Line;
class Demo implements Comparable<Demo>{
private String word;
private int num;
public Demo(String word, int num) {
// TODO Auto-generated constructor stub
this.word = word;
this.num = num;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int compareTo(Demo o) {
if(this.getNum() > o.getNum()) {
return -1;
} else if(this.getNum() < o.getNum()) {
return 1;
} else if(this.getNum() == o.getNum()) {
return this.getWord().compareTo(o.getWord());
}
return 0;
}
public String toString() {
return this.word + "=" + this.num;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Demo> list = new ArrayList<Demo>();
Map<String, Integer> map = new HashMap<String, Integer>();
String string;
while(scanner.hasNextLine()) {
string = scanner.nextLine();
if(string.length() == 0) {
continue;
}
if(string.equals("!!!!!")) {
break;
}
string = string.replace('!', ' ');
string = string.replace('.', ' ');
string = string.replace(',', ' ');
string = string.replace(':', ' ');
string = string.replace('*', ' ');
string = string.replace('?', ' ');
string = string.toLowerCase();
String[] words = string.split(" ");
for(int i = 0; i < words.length; i++) {
if(words[i].length() == 0) {
continue;
}
//System.out.println(words[i]);
if(map.containsKey(words[i])) {
map.put(words[i], map.get(words[i]) + 1);
} else {
map.put(words[i], 1);
}
}
}
for(String word : map.keySet()) {
list.add(new Demo(word, map.get(word)));
}
Collections.sort(list);
System.out.println(list.size());
for(int i = 0; i < list.size() && i < 10; i++) {
System.out.println(list.get(i));
}
scanner.close();
}
}
7-2 约瑟夫环问题-hebust (25 分)
约瑟夫环问题
约瑟夫环是一个数学的应用问题:已知n个人(以编号a,b,c...分别表示)围坐在一张圆桌周围。从编号为1的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。
输入格式:
固定为2行,第一行为m,第二行为n个人的名称列表,用英文字母代表,元素直接使用英文逗号 , 分开
输出格式:
一行,为出列元素序列,元素之间使用英文逗号 , 分开【注意:末尾元素后没有逗号】
输入样例:
在这里给出一组输入。例如:
3
a,b,c,d,e,f,g
输出样例:
在这里给出相应的输出。例如:
c,f,b,g,e,a,d
解析:
简单约瑟夫环,取余运算。
AC代码:
import java.util.ArrayList;
import java.util.Scanner;
import org.omg.CORBA.VersionSpecHelper;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
int n = scanner.nextInt();
scanner.nextLine();
String string = scanner.nextLine();
String[] persons = string.split(",");
boolean[] vis = new boolean[persons.length];
int pos = -1;
while(list.size() < persons.length - 1) {
//System.out.println(list.size());
for(int i = 0; i < n; i++) {
pos = (pos + 1) % persons.length;
while(vis[pos]) {
pos = (pos + 1) % persons.length;
}
}
//System.out.println("pos = " + pos);
list.add(persons[pos]);
vis[pos] = true;
}
//System.out.println(list);
for(int i = 0; i < vis.length; i++) {
if(!vis[i]) {
list.add(persons[i]);
}
}
System.out.print(list.get(0));
for(int i = 1; i < list.size(); i++) {
System.out.print("," + list.get(i));
}
scanner.close();
}
}
最后
以上就是自信冰淇淋为你收集整理的《Java程序设计》 容器3的全部内容,希望文章能够帮你解决《Java程序设计》 容器3所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复