首页 > 其他分享 >课堂练习01题目:计算最长英语单词链总结

课堂练习01题目:计算最长英语单词链总结

时间:2023-02-28 17:12:56浏览次数:39  
标签:01 英语单词 课堂练习 word System println new total out

 一、题目内容:

大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N 个不同的英语单词, 我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。

最长的定义是:最多单词数量,和单词中字母的数量无关。

二、题目要求:

1、统一输入文件名称:input1.txt, input2.txt

2、统一输出文件名称:output1.txt,output2.txt

3、程序需要考虑下列异常状况:

(1)例如,文件不存在,你的程序会崩溃么,还是能优雅地退出并给用户提示信息?

(2)如果文件没有任何单词、只有一个单词、没有可以首尾相连的单词,程序应该如何输出?

(3)如果输入文件有一万个单词,你的程序能多快输出结果?

 

思路设计:

1、先构思程序步骤:导入文件、提取单词、提取单词首和尾、比较结果存储、导出最长链。

2、代码实现:

先利用

1 File f1 = new File("src/input3919.txt");
2         BufferedReader br = new BufferedReader(new FileReader(f1));
3         //定义个字符串用来保存每次读取的数据
4         String len;
5         StringBuffer sb = new StringBuffer();
6         while ((len = br.readLine()) != null) {
7            // System.out.println(len);
8                 sb.append(len);//将文件内容存入sb中
9         }

提取文件中的内容

然后利用

 1  String str = sb.toString().toLowerCase();
 2  String[] strings = str.split("[^a-zA-Z\\']+");

对内容单词进行分割并统一大小写

随后利用substring函数进行首尾提取,比较并储存结果。利用.write()函数写入文件。

 

完成代码:

 1 package com.text;
 2 
 3 import org.omg.CORBA.WStringSeqHelper;
 4 
 5 import java.io.*;
 6 import java.util.ArrayList;
 7 import java.util.Objects;
 8 
 9 public class file4 {
10     public static void main(String[] args) throws Exception {
11         File file = new File("src/input3919.txt");
12         if (file.exists() && file.length() == 0) {
13             System.out.println("  文件为空!");
14             System.exit(0);
15         } else if (achieve() == 1) {
16             System.out.println("  文件只有一個單詞!");
17             System.exit(0);
18         } else {
19             achieve();
20         }
21     }
22 
23     public static long achieve() throws Exception {
24         // ArrayList<String> list = new ArrayList<String>();
25         ArrayList<String> list2 = new ArrayList<String>();//存储遍历结果
26 
27         File f1 = new File("src/input3919.txt");
28         BufferedReader br = new BufferedReader(new FileReader(f1));
29         //定义个字符串用来保存每次读取的数据
30         String len;
31         StringBuffer sb = new StringBuffer();
32         while ((len = br.readLine()) != null) {
33            // System.out.println(len);
34                 sb.append(len);//将文件内容存入sb中
35         }
36         //不区分大小写
37         String str = sb.toString().toLowerCase();
38         String[] strings = str.split("[^a-zA-Z\\']+");
39 
40         String tail, head, word;
41         long total = 1, max = 1;
42 
43         for (int i = 0; i < Objects.requireNonNull(strings).length; i++) {
44             ArrayList<String> list1 = new ArrayList<String>();//存储遍历结果
45             total = 1;
46 
47             word = strings[i];
48             list1.add(word);
49             tail = word.substring(word.length() - 1);
50 
51              //System.out.println(tail);
52 
53             for (int j = i + 1; j < Objects.requireNonNull(strings).length; j++) {
54                 word = strings[j];
55                 head = word.substring(0, 1);
56                 //System.out.println(head);
57 
58                 if (tail.equals(head)) {
59                     list1.add(word);
60                     //System.out.println(total);
61                     tail = word.substring(word.length() - 1);
62                     total++;
63                 } else {
64                    // System.out.println(total);
65                     if (total > max)
66                     {
67                         list2 = list1;
68                         max = total;
69                     }
70                     break;
71                 }
72                 if(j == Objects.requireNonNull(strings).length - 1)
73                 {
74                     if (total > max)
75                     {
76                         list2 = list1;
77                         max = total;
78                     }
79                 }
80             }
81         }
82         System.out.println(max);
83 
84         File file = new File("src/output.txt");
85         FileWriter out = new FileWriter(file);
86         for (int i = 0; i < list2.size(); i++) {
87             System.out.println(list2.get(i));
88             out.write(list2.get(i) + " ");
89         }
90         br.close();
91         out.close();
92         return max;
93     }
94 }

 

标签:01,英语单词,课堂练习,word,System,println,new,total,out
From: https://www.cnblogs.com/fan-wang/p/17165150.html

相关文章

  • [洛谷]P5401 [CTS2019] 珍珠 题解
    [洛谷]P5401[CTS2019]珍珠题解题意概述有\(D\)种珍珠,每种有无限颗,现在等概率的从\(D\)种珍珠中抽\(n\)次珍珠,每次抽\(1\)个珍珠,记第\(i\)种珍珠最后一共抽......
  • 每日知识小复习-01
    昨天的小测验中,我发现JAVA的一些基础知识我遗忘的很多,因此写几篇笔记,温习一下以前的知识,以后再复习,也更加方便。一.StringBuffer和StringBuilder类为什么要用StringB......
  • vs2010 升级 vs2015问题
    fatalerrorLNK1295:“/OPT:NOREF”与“/LTCG:incremental”规范不兼容;链接时不使用“/LTCG:incremental” 项目属性-》链接器-》优化-》引用-》是项目属性-》链接器-》......
  • 单个表空间文件个数达到上限 ORA-01686
    #问题概述因在oracle数据库表空间管理中的时候报ORA-01686:max#files(1023)reachedforthetablespaceGPRSSQL>altertablespaceGPRSadddatafile'+DATADG'......
  • L1-011 A-B【团体程序设计天梯赛-练习集】
    L1-011A-B题要求你计算A−B。不过麻烦的是,A和B都是字符串——即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A−B。输入格式:输入在2行中先后......
  • BUUctf ciscn_2019_n_1
    BUUciscn_2019_n_1首先file,是64位elfIDA,发现main函数会调用func函数,而func中如果v2=11.28125,就会cat/flag,并且上面有个gets,存在栈溢出漏洞压栈的过程是v1的44字节,然......
  • BUUctf warmup_csaw_2016
    BUUwarmup_csaw_2016首先file,发现是64位ELF文件。IDA发现main函数中return了get函数,存在溢出点。查看文件中的字符串,发现有'catflag.txt'双击后发现在sub_40060D......
  • 轻量级CI/CD发布部署环境搭建及使用_01_基本介绍
    轻量级CI/CD发布部署环境搭建及使用_01_基本介绍授人以鱼不如授人以渔,如果说的别人都没明白,说明自己实际也不是太明白 最终实现效果如图1,选择相应环境下的项目,执行构......
  • (原创)【B4A】一步一步入门05:控件、公有属性、水平锚定、垂直锚定(控件篇01)
    一、前言前面的教程,已经完整讲述了用B4A开发安卓APP从新建项目到编译发布的完整流程。从本篇开始,我们将会从B4A的细节处着手,一步一步掌握B4A。从本篇开始的子系列为“控......
  • Exchange 2013 清空邮箱
    在某些应用场景中,需要清空用户邮箱的所有数据。如果使用Outlookwebapp或者Outlook的邮件删除方式,对数以千计的邮件来说,实在不是一个好办法。exchange管理员可以使用“Se......