单词阵列游戏

版权声明:博客内容为本人自己所写,请勿转载。 https://blog.csdn.net/weixin_42805929/article/details/82628127

单词阵列游戏:

将一组单词排列在一个列表中,要求:使得任何字母的首字母
与其前面单词的尾字母相同。如果列表中的单词按照要求排列,返回1;否则为-1
其中,所有的单词字母必须为小写,单词数组arr长度在[2,100]中

import java.util.*;

/**
 * 单词阵列游戏:将一组单词排列在一个列表中,要求:使得任何字母的首字母
 * 与其前面单词的尾字母相同。如果列表中的单词按照要求排列,返回1;否则为-1
 * 其中,所有的单词字母必须为小写,单词数组arr长度在[2,100]中
 *
 */
public class Main4 {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] st = sc.nextLine().split(",");
        int i = check(st);
        System.out.println(i);
    }

    public static int check(String[] arr) {
        int flag = -1;
        int k = 0;
        if (k < arr.length) {
            char ch = arr[k].charAt(k);
            if (Character.isLowerCase(ch) && arr.length >= 2 && arr.length <= 100) {
                for (int i = 1; i < arr.length; i++) {
                    String temp = arr[i];
                    String[] str = temp.split("");
                    System.out.println(str[0] + "---");
                    for (int j = i - 1; j >= 0; j--) {
                        String[] s = arr[j].split("");
                        if (s[s.length - 1].equals(str[0])) {
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 1) {
                        break;
                    }
                }
            }
        } else {
            k++;
        }
        return flag;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42805929/article/details/82628127