博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
课堂练习-词语接龙
阅读量:6943 次
发布时间:2019-06-27

本文共 4333 字,大约阅读时间需要 14 分钟。

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

例如, 文件里有:

Apple

Zoo

Elephant

Under

Fox

Dog

Moon

Leaf

Tree

Pseudopseudohypoparathyroidism

最长的相连英语单词串为:  apple - elephant – tree,  输出到文件里面,是这样的:

Apple

Elephant

Tree

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

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

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

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

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

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

要求将设计思想、代码实现、实现截图、个人总结以博文的形式发表。

设计思想:

     将单词存到list容器中,之后先获取到第一个单词的首、尾字母,之后再循环中获取下一个单词的首尾字母,让第一个单词的尾字母与第二个单词的尾字母比较,如果相同,则将第二个单词的首位字母赋值给第一个单词的首位字母的变量,以此类推。

实验代码:

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.Writer;import java.text.DecimalFormat;import java.text.NumberFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Scanner;public class Text_2 {static int N=5;static String b[]=new String[500];public static void main(String[] args)     {                        String sz=writeFromFile.readTxtFile("input3.txt");        String ltxt=null;        if(sz==null)        {            System.out.println("找不到指定的文件");         }        else if(sz=="kong")        {            System.out.println("文件为空!");          }                else        {            System.out.println(ltxt=StatList1(sz));                         select(b);        }                try {        writeFromFile.daochu(ltxt);        } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();        }                    }public static int woor(String a)    {        int n=0;        File ctoFile = new File("stopword.txt");        InputStreamReader rdCto;        try {            rdCto = new InputStreamReader(new FileInputStream(ctoFile));            BufferedReader bfReader = new BufferedReader(rdCto);            String txtline = null;            try {                while ((txtline = bfReader.readLine()) != null)                 {                    if(txtline.equals(a))                        {                            n=1;                        }                }                bfReader.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }                     } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return n;    }public static ArrayList
getFiles(String path) { ArrayList
files = new ArrayList
(); File file = new File(path); File[] tempList = file.listFiles(); for (int i = 0; i < tempList.length; i++) { if (tempList[i].isFile()) { files.add(tempList[i].toString()); } if (tempList[i].isDirectory()) { } } return files; }public static String StatList1(String str) { StringBuffer sb = new StringBuffer(); HashMap
has = new HashMap
(); // 打开一个哈希表 String[] slist = str.split("[^a-zA-Z\']+"); for (int i = 0; i < slist.length; i++) { if (!has.containsKey(slist[i])) { has.put(slist[i], 1); } else { has.put(slist[i],has.get(slist[i])+1 ); } } Iterator
iterator = has.keySet().iterator(); String a[]=new String[500]; int s[]=new int[500]; int judge; int n=400; for(int i=0;i
list=new ArrayList<>(); int q=0; int n0=0; for(int i=0;i
"; System.out.println(list.get(i)+"->"); } try { daochu(l); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void daochu(String a) throws IOException { File file =new File("output1.txt"); Writer out =new FileWriter(file); String data=a; out.write(data); out.close(); } }

实验截图:

转载于:https://www.cnblogs.com/liujinxin123/p/10994047.html

你可能感兴趣的文章
python 多进程与子进程
查看>>
Git常用命令
查看>>
自开发Web应用和SAP Customer Data Cloud Identity服务的集成
查看>>
HanLP Android 示例
查看>>
推荐四十多条纯干货 Java 代码优化建议
查看>>
「镁客·请讲」太平洋未来科技李建亿:深耕AR技术,布局垂直领域
查看>>
如何用纯 CSS 创作一种侧立图书的特效
查看>>
中软酒店管理系统CSHIS操作手册_数据结构_数据字典
查看>>
跳出弹窗页面禁止滚动(PC端和手机端)
查看>>
HTML5/CSS3鼠标悬停动画菜单按钮
查看>>
Android Studio打包错误(Cannot merge new index 67578 into a non-jumbo instruction!)
查看>>
SLS机器学习介绍(03):时序异常检测建模
查看>>
4.1ASP.NET Core请求过程「深入浅出ASP.NET Core系列」
查看>>
安装elasticsearch中文切词插件hanlp
查看>>
Redis 的 KEYS 命令引起 RDS 数据库雪崩,宕机 2 次,造成几百万损失
查看>>
点播转码相关常见问题及排查方式
查看>>
gc.collect()清内存
查看>>
如何在HTTPS里调用HTTP资源不出现提示框
查看>>
Jenkins 2.173 发布,开源持续集成引擎
查看>>
《文科生数据科学上手指南》分享
查看>>