java题目集合第六部分(pta)

目录

7-1 jmu-Java-05集合-01-ListIntegerStack

main方法说明

思考:

输入样例

输出样例

7-2 jmu-Java-05集合(泛型)-10-GeneralStack

1. 编写一个通用的GeneralStack接口,接口中的操作对任何引用类型的数据都适用。

2.定义GeneralStack的实现类ArrayListGeneralStack

3.定义Car对象

4.main方法说明

输入样例

输出样例

7-3 jmu-Java-05集合-4-倒排索引

输入说明

输出说明

输入样例

输出样例

7-4 jmu-Java-m06 统计一篇英文文章中出现的不重复单词的个数

输入格式:

输出格式:

输入样例:

输出样例:

7-5 sdut-String+array(LinkedHashMap) 读中国载人航天史,汇航天员数量,向航天员致敬(1)

输入格式:

输出格式:

输入样例:

输出样例:

7-6 sdut-Map(dict)-String--单词和字符鉴别器

输入格式:

输出格式:

输入样例:

输出样例:


7-1 jmu-Java-05集合-01-ListIntegerStack

分数 15

全屏浏览题目

切换布局

作者 郑如滨

单位 集美大学

定义IntegerStack接口,该接口描述了一个存放Integer的栈的常见方法:


public Integer push(Integer item); //如item为null,则不入栈直接返回null。否则直接入栈,然后返回item。 public Integer pop(); //出栈,如栈为空,则返回null。 public Integer peek(); //获得栈顶元素,如栈顶为空,则返回null。注意:不要出栈 public boolean empty(); //如过栈为空返回true public int size(); //返回栈中元素数量

定义IntegerStack的实现类ArrayListIntegerStack,内部使用ArrayList存储。该类中包含:

构造方法:
在无参构造方法中新建ArrayList或者LinkedList,作为栈的内部存储。
思考:查询JDK文档,尝试说明本题到底使用哪个List实现类最好。

方法:
public String toString() //用于输出List中的内容,可直接调用List的toString()方法。可用System.out.println(list)进行输出。

提示:

  1. 不建议使用top指针。最好直接复用List实现类中已有的方法。
  2. pop时应将相应的元素从列表中移除。

main方法说明

  1. 建立ArrayIntegerStack对象
  2. 输入m个值,均入栈。每次入栈均打印入栈返回结果。
  3. 输出: 栈顶元素,输出是否为空,然后输出size.
  4. 输出栈中所有元素(调用其toString()方法)
  5. 输入x,然后出栈x次,每次均打印出栈的对象。
  6. 输出:栈顶元素,输出是否为空,输出size。注意:这里的输出栈顶元素,仅输出、不出栈。
  7. 输出栈中所有元素(调用其toString()方法)。注意:返回null,也要输出。

思考:

如果使用LinkedList来实现IntegerStack,怎么实现?测试代码需要进行什么修改?

输入样例

5
1 3 5 7 -1
2

输出样例

1
3
5
7
-1
-1,false,5
[1, 3, 5, 7, -1]
-1
7
5,false,3
[1, 3, 5]
import java.util.*;
interface IntegerStack{
    public Integer push(Integer item);
    public Integer pop();
    public Integer peek();
    public boolean empty();
    public int size();
}
class ArrayListIntegerStack implements IntegerStack{
    ArrayList a;
   ArrayListIntegerStack(){
       a=new ArrayList();
   }
    public Integer push(Integer item){//入栈
        if(item!=null){
            a.add(item);
            return item;
        }
        return null;
    }
    public Integer pop(){//出栈
        if(a.size()!=0){
            Integer b=a.get(a.size()-1);
            a.remove(a.size()-1);
              return b;
        }
        return null;
    }
    public Integer peek(){
        if(a.size()!=0){
        int s=a.size();
        return a.get(s-1);
        }
        return null;
    }
    public boolean empty(){
        if(a.size()==0)
            return true;
        return false;
    }
    public int size(){
        return a.size();
    }
}
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n1=sc.nextInt();
        ArrayListIntegerStack ar=new ArrayListIntegerStack();
        for(int i=0;i

7-2 jmu-Java-05集合(泛型)-10-GeneralStack

分数 15

全屏浏览题目

切换布局

作者 郑如滨

单位 集美大学

以前定义的IntegerStack接口,只能用于存放Integer类型的数据。然而对于栈来说,不管内部存放的是什么类型的数据,基本操作与元素的具体类型无关。

1. 编写一个通用的GeneralStack接口,接口中的操作对任何引用类型的数据都适用。

一旦定义完毕,只能存放一种类型的数据,比如只能存放String或只能存放Integer。GeneralStack接口方法如下:


push(item); //如item为null,则不入栈直接返回null。 pop(); //出栈,如为栈为空,则返回null。 peek(); //获得栈顶元素,如为空,则返回null. public boolean empty();//如为空返回true public int size(); //返回栈中元素数量

2.定义GeneralStack的实现类ArrayListGeneralStack

内部使用ArrayList对象存储,属性名为list

方法:
public String toString()//该方法的代码为return list.toString();

提示:

  1. 不用使用top指针。
  2. 直接复用ArrayList中已有的方法。
  3. pop时应将相应的元素从ArrayList中移除。
  4. 代码中不要出现类型不安全的强制转换

3.定义Car对象

属性:

private int id;
private String name;

方法:Eclipse自动生成setter/getter,toString方法。

4.main方法说明

  1. 输入选项,有quit, Integer, Double, Car4个选项。如果输入quit,则直接退出。否则,输入整数m与n。m代表入栈个数,n代表出栈个数。然后声明栈变量stack
  2. 输入Integer,打印Integer Test。建立可以存放Integer类型的ArrayListGeneralStack。入栈m次,出栈n次。打印栈的toString方法。最后将栈中剩余元素出栈并累加输出。
  3. 输入Double ,打印Double Test。剩下的与输入Integer一样。
  4. 输入Car,打印Car Test。其他操作与Integer、Double基本一样。只不过最后将栈中元素出栈,并将其name依次输出。

2、3、4步骤做完都要使用代码System.out.println(stack.getClass().getInterfaces()[0]);打印标识信息

特别注意:如果栈为空的时候继续出栈,则返回null

输入样例

Integer
5
2
1 2 3 4 5
Double
5
3
1.1 2.0 4.9 5.7 7.2
Car
3
2
1 Ford
2 Cherry
3 BYD
quit

输出样例

Integer Test
push:1
push:2
push:3
push:4
push:5
pop:5
pop:4
[1, 2, 3]
sum=6
interface GeneralStack
Double Test
push:1.1
push:2.0
push:4.9
push:5.7
push:7.2
pop:7.2
pop:5.7
pop:4.9
[1.1, 2.0]
sum=3.1
interface GeneralStack
Car Test
push:Car [id=1, name=Ford]
push:Car [id=2, name=Cherry]
push:Car [id=3, name=BYD]
pop:Car [id=3, name=BYD]
pop:Car [id=2, name=Cherry]
[Car [id=1, name=Ford]]
Ford
interface GeneralStack
import java.util.*;
interface GeneralStack{
    public abstract E push(E item);
    public abstract E pop();
    public abstract E peek();
    public abstract boolean empty();
    public abstract int size();
}
class ArrayListGeneralStack implements GeneralStack{
    ArrayList list;
    ArrayListGeneralStack(){
        list=new ArrayList<>();
    }
    public  E push(E item){
        if(item!=null){
            list.add(item);
            return item;
        }
        return null;
    }
    public  E pop(){
        if(list.size()!=0){
            E b=list.get(list.size()-1);
            list.remove(list.size()-1);
            return b;
        }
        return null;
    }
    public E peek(){
        if(list.size()!=0){
            return list.get(list.size()-1);
        }
        return null;
    }
    public boolean empty(){
        if(list.size()==0)
            return true;
        return false;
    }
    public int size(){
        return list.size();
    }
    public String tostring(){
        return list.toString();
    }
}
class car{
    private int id;
    private String name;
    car(int id,String name){
        this.id=id;
        this.name=name;
    }
    public void set1(int id){
        this.id=id;
    }
    public int get1(){
        return this.id;
    }
    public void set2(String name){
        this.name=name;
    }
    public String get2(){
        return this.name;
    }
    public String toString(){
        return "Car [id="+id+", name="+name+"]";
    }
}
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            String s=sc.next();
            if(s.equals("Integer")){
                System.out.println("Integer Test");
                int count=sc.nextInt();
                int poptime=sc.nextInt();
                ArrayListGeneralStack a1=new ArrayListGeneralStack<>();
                for(int i=0;i a2=new ArrayListGeneralStack<>();
                for(int i=0;i a3=new ArrayListGeneralStack<>();
                int count=sc.nextInt();
                int poptime=sc.nextInt();
                for(int i=0;i0){
                    int size=a3.size();
                    for(int i=0;i

7-3 jmu-Java-05集合-4-倒排索引

分数 20

全屏浏览题目

切换布局

作者 郑如滨

单位 集美大学

对若干行文字建立倒排索引(根据单词找到所在行号)。
然后根据关键字,在倒排索引查找进行查找,找到包含所有该关键字所在的行数并输出。

输入说明

  1. 若干行英文,以!!!!!为结束。
  2. 输入一行查询关键字,以1个空格为分隔

输出说明

  1. 输出所创建倒排索引。索引的key按照字母升序,索引的value按照行号升序
  2. 输出查询结果。如果找到,输出行集与行集内每一行的内容,如果没找到输出found 0 results

输入样例

where are you from are you ok
this is a test 
that is an apple
there are lots of apples you eat it
who are you
!!!!!
you are
eat
you test
abc

输出样例

a=[2]
an=[3]
apple=[3]
apples=[4]
are=[1, 4, 5]
eat=[4]
from=[1]
is=[2, 3]
it=[4]
lots=[4]
of=[4]
ok=[1]
test=[2]
that=[3]
there=[4]
this=[2]
where=[1]
who=[5]
you=[1, 4, 5]
[1, 4, 5]
line 1:where are you from are you ok
line 4:there are lots of apples you eat it
line 5:who are you
[4]
line 4:there are lots of apples you eat it
found 0 results
found 0 results
import java.util.*;
class danci{
    private int add;
    private String name;
    danci(int add,String name){
        this.add=add;
        this.name=name;
    }
    public int getadd(){
        return this.add;
    }
    public String getname(){
        return this.name;
    }
}
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String str;
        String a[];
        String c[]=new String[101];
      ArrayList b=new ArrayList<>();
        int hang=0;
        while(sc.hasNext()){
            str=sc.nextLine();
            if(str.equals("!!!!!"))
                break;
            a=str.split(" ");
            hang++;
            c[hang]=str;
            for(int i=0;i0){
                    min=j;
                }
                else if(b.get(min).getname().compareTo(b.get(j).getname())==0&&b.get(min).getadd()>b.get(j).getadd()){
                    min=j;
                }
            }
            danci t=b.get(i);
            b.set(i,b.get(min));
            b.set(min,t);
        }
        System.out.print(b.get(0).getname()+"=["+b.get(0).getadd());
        for(int i=1;i

7-4 jmu-Java-m06 统计一篇英文文章中出现的不重复单词的个数

分数 10

全屏浏览题目

切换布局

作者 郑如滨

单位 集美大学

输入一篇英文文章,碰到"!!!!!"的时候停止,输出文章中出现的不重复单词的个数(注意:单词不区分大小写,如:The和the为一个单词)

输入格式:

一篇英文文章,以"!!!!!"结尾

输出格式:

不重复单词的个数

输入样例:

Unmanned aerial vehicles have been adopted in the inspection of violations Procurators will file public interest litigations against perpetrators who will need to replant trees take down illegal buildings control pollution and compensate environment-related losses  
The Yellow River is the second longest river in China It originates in the northwestern province of Qinghai and runs through nine provinces and autonomous regions in western central north and eastern China  
!!!!!

输出样例:

55
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        Set s=new HashSet<>();
        while(sc.hasNext()){
            String str=sc.next();
            if(str.equals("!!!!!")){
                break;
            }
            s.add(str.toLowerCase());
        }
        System.out.println(s.size());
    }
}

7-5 sdut-String+array(LinkedHashMap) 读中国载人航天史,汇航天员数量,向航天员致敬(1)

分数 10

全屏浏览题目

切换布局

作者 周雪芹

单位 山东理工大学

1986年,中国实施“863”计划,航天技术列入其中。以载人飞船开始起步,最终建成我国的空间站。
1992年9月21日,中国实施载人航天工程,并确定了三步走的发展战略:第一步,发射载人飞船,建成初步配套的试验性载人飞船工程。第二步,突破载人飞船和空间飞行器的交会对接技术,利用载人飞船技术改装、发射一个空间实验室。第三步,建造载人空间站。

在长期的奋斗中,我国航天工作者不仅创造了非凡的业绩,而且铸就了特别能吃苦、特别能战斗、特别能攻关、特别能奉献的载人航天精神。载人航天精神,是“两弹一星”精神在新时期的发扬光大,是我们伟大民族精神的生动体现,永远值得全党、全军和全国人民学习。


截至2021年4月,历任航天英雄名字如下:
杨利伟(神舟五号)
费俊龙、聂海胜(神舟六号)
翟志刚、景海鹏、刘伯明(神舟七号)
景海鹏、刘旺、刘洋(神舟九号)
聂海胜、张晓光、王亚平(神舟十号)
景海鹏、陈东(神舟十一号)

会编程的小伙伴们,请以他们出征太空的先后顺序,统计一下航天英雄们出征太空的次数,以实际行动向航天员们致敬!

输入格式:

每次航天飞船的编号为一行读入数据,分别读入每次飞上太空的航天英雄的姓名,名字中间有一个空格分隔。
最后一行为“end“,表示输入结束。

提示:目前,中国航天员的数量小于20。

输出格式:

以出征太空的先后顺序,统计航天英雄们出征太空的次数。
每位航天员占一行,航天员姓名与出征次数中间有一个空格。

输入样例:

杨利伟
费俊龙 聂海胜
翟志刚 景海鹏 刘伯明
景海鹏 刘旺 刘洋
聂海胜 张晓光 王亚平
景海鹏 陈东
end

输出样例:

杨利伟 1
费俊龙 1
聂海胜 2
翟志刚 1
景海鹏 3
刘伯明 1
刘旺 1
刘洋 1
张晓光 1
王亚平 1
陈东 1
import java.util.*;
public class Main{
    public static void main(String[] args){
        Map m=new LinkedHashMap<>();
        Scanner sc=new Scanner(System.in);
        String str=sc.next();
        while(!str.equals("end")){
            if(!m.containsKey(str)){
                m.put(str,1);
            }
            else{
                m.put(str,m.get(str)+1);
            }
            str=sc.next();
        }
        Set s=m.keySet();
        for(String s1:s){
            System.out.println(s1+" "+m.get(s1));
        }
    }
}

7-6 sdut-Map(dict)-String--单词和字符鉴别器

分数 10

全屏浏览题目

切换布局

作者 周雪芹

单位 山东理工大学

读入包含若干个单词的文本数据,将所有内容转换为大写,统计每个单词与该单词的出现次数。此外,还需要统计每个字符及其出现次数(不包括空格)。

输入格式:

若干行的单词,以空格作为分隔符,每行单词数量不定。

遇到一行数据“0000”,读取数据结束。(字符串“0000”不计入上述统计数据)

输出格式:

全部数据读取完成,输出如下信息:

(1)出现次数最多的单词及其出现次数;若次数相同,输出字典序最大的单词。

(2)出现次数最少的单词及其出现次数;若次数相同,输出字典序最小的单词。

(3)每个字母,按A到Z的顺序以及次数。

其中,单词和字母占10个字符位置,左对齐;计数(即:出现次数)占8位,右对齐。

提示:

Java语言,建议使用HashMap存储数据;
Python语言,建议使用dict存储数据。

输入样例:

I would rather have had one breath of her hair
one KISS of her mouth one touch of her hand than an eternity without it
I WOULD rathER haVE HAD oNe BreatH Of Her Hair
ONE kiss of her mouth one touch of her hand than an eternity WITHOUT IT
0000

输出样例:

ONE              6
AN               2
A               16
B                2
C                2
D                6
E               22
F                6
G                0
H               26
I               12
J                0
K                2
L                2
M                2
N               14
O               20
P                0
Q                0
R               16
S                4
T               20
U                8
V                2
W                4
X                0
Y                2
Z                0
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        Map map=new TreeMap<>();
        String str;
        int danci[]=new int[30000];
        while(sc.hasNext()){
            str=sc.nextLine();
            if(str.equals("0000"))
                break;
            str=str.toUpperCase();//将文本全部转为大写
            for(int i=0;i> s1=map.entrySet();
        for(Map.Entrys2:s1){  //这里我们使用增强for
            String key=s2.getKey();//对应单词
            int value=s2.getValue();//出现次数
            if(value>=Max){//因为要输出大的
                Max=value;
                max=key;
            }
            else if(value

题目集合到这里就结束了,如果对代码有困惑可以私聊博主,我会尽力解答的!

你可能感兴趣的:(java,开发语言)