输出最长字符串链

package com.English1;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class English1 {
    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("D:\\Eclipse workspace\\English\\input1.txt");// 读取文件
        if (!file.exists()) {// 如果文件打不开或不存在则提示错误
            System.out.println("文件不存在");
            return;
        }else {
            if(file.exists() && file.length() == 0) {  
                System.out.println("文件为空!");  
                return;
            }  
        }
        long startTime = System.currentTimeMillis();
        String[] strs=new String[1000000];
        Scanner x = new Scanner(file);
        int i=0;
        boolean flag=false;
        while(x.hasNextLine()) {
            String[] str=x.nextLine().split("\\W+");
            for(int ms=0;ms<str.length;ms++) {
                if(!str[ms].equals("")&&str[ms].length()>2) {
                    flag=false;
//                    System.out.println(str[ms]);
                    if(i!=0) {
                        for(int t=0;t<i;t++) {
                            if(!str[ms].equals(strs[t])) {
                                flag=true;
                            }
                        }
                    }else {
                        flag=true;
                    }
                    
                    if(flag) {
                        strs[i]=str[ms];
                        i++;
                    }
                    
                }
                
            }
        }
        if(i==1) {
            System.out.println("该文件只有一个单词!无法实现词语接龙");
        }
        String sentence = "";
        String word="";
        String max="";
        for(int m=0;m<i;m++) {
            sentence = strs[m];
            word = sentence;
            for(int j=m+1;j<i;j++) {
                if(strs[j].toLowerCase().subSequence(0, 1).equals(word.toLowerCase().subSequence(word.length()-1, word.length()))) {
                    word = strs[j];
                    sentence+="-"+word;
                }
            }
            
            if(sentence.indexOf("-")!=-1) {
                if(sentence.length()>max.length()) {
                    max = sentence;
                }
//                System.out.println(sentence);
            }
            
        }
        long endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime+"ms");
        System.out.println(i);
        if(max.length()!=0) {
            System.out.println(max);
        }else {
            System.out.println("没有首尾相连");
        }
        
    }
}

设计思路:

  首先从文件中读取,然后将单词写入到一个单词数组里,然后对数组进行操作。首先获取每个单词的第一个字母和最后一个字母,然后就判断是否是首尾相连,如果是首尾相连,再依次写进对象里面,最后将对象写进输出文件中。

截图:

 

转载于:https://www.cnblogs.com/liyuchao/p/10995069.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是一种用 C 语言实现的方法: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LEN 20 struct node { char data[MAX_LEN + 1]; struct node *next; }; int main() { int n, max_len = 0; struct node *head, *p, *max_node; char str[MAX_LEN + 1]; scanf("%d", &n); head = (struct node *)malloc(sizeof(struct node)); head->next = NULL; p = head; for (int i = 0; i < n; i++) { scanf("%s", str); p->next = (struct node *)malloc(sizeof(struct node)); strcpy(p->next->data, str); p = p->next; p->next = NULL; if (strlen(str) > max_len) { max_len = strlen(str); max_node = p; } } printf("%s\n", max_node->data); return 0; } ``` 这段代码实现了输入字符串链式存储,并输出最大字符串的功能。 ### 回答2: 要编写程序输入n个字符串链式存放,并输出其中最大的字符串。实现这个功能可以采用以下步骤: 1. 首先,可以定义一个字符串变量max_str,用来保存当前最大的字符串。 2. 接下来,让用户输入一个整数n,表示接下来要输入的字符串的个数。 3. 然后,使用一个循环,让用户逐个输入字符串,并与max_str比较度大小,并更新max_str的值。 4. 循环结束后,输出max_str,即为最大的字符串。 下面给出程序示例: ```python max_str = '' # 初始化最大字符串为空 n = int(input("请输入字符串的个数:")) for i in range(n): string = input("请输入字符串:") if len(string) > len(max_str): max_str = string print("最大字符串是:", max_str) ``` 这个程序中,首先使用一个循环输入n个字符串,并与max_str比较。如果当前输入的字符串度大于max_str的度,则更新max_str的值。最后输出max_str即为最大字符串。 希望对你有帮助! ### 回答3: 要编写程序输入n个字符串链式存放,每个字符串度不超过20,输出最大字符串,可以采用以下步骤: 首先,定义一个变量max_string,用于存储最大字符串。初始化为一个空字符串。 接着,使用循环从1到n进行遍历,每次迭代输入一个字符串。 对于每个输入的字符串,可以使用len()函数获取其度。将获取到的度与max_string的度进行比较。 如果当前输入的字符串度大于max_string的度,将max_string更新为当前输入的字符串。 循环结束后,max_string即为最大的字符串。 最后,输出max_string即可。 下面是一种可能的实现方式: ```python n = int(input("请输入字符串的个数:")) max_string = '' for i in range(n): string = input("请输入第{}个字符串:".format(i+1)) if len(string) > len(max_string): max_string = string print("最大的字符串是:", max_string) ``` 这个程序会先要求输入字符串的个数,然后依次输入每个字符串,并通过比较不断更新max_string为度最大的字符串。最后输出最大的字符串。 注意:在实际编写程序时,需要进行输入的格式验证,确保输入的字符串个数与字符串度都符合要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值