根据特殊EOF的序列判断比赛输赢(洛谷P1042题题解,Java语言描述)

题目要求

P1042题目链接

在这里插入图片描述
在这里插入图片描述

分析

本题的EOF显然是’E’,其后就算有任何内容也都与我们需要的数据无关啦,这是一定要记住的。

不能直接将scanner.nextLine()的数据用于算法,因为要处理两次(除非将两个一起进行,但没必要啊)。
存在LinkedList中即可。

单独设置一个函数进行胜负判断,因为不需要进行判断谁输谁赢,赢就完事,所以可以这么写:

    private static boolean judgeEnding(int tempPointW, int tempPointL, int endCondition) {
        if ((tempPointW-tempPointL >= 2 && tempPointW >= endCondition) ||
                (tempPointL-tempPointW >= 2 && tempPointL >= endCondition)) {
            return true;
        }
        return false;
    }

我的策略是当读的一行String里有“E”的时候(用contains()方法),取一下indexOf(),进而取一下substring(),防止EOF以后的数据影响结果,并将循环结束。

注意下面的结构会“死循环”:

while(scanner,hasNextLine()) {
    String s = scanner.nextLine();
}

这个该死的结构一般只能避免使用,所以EOF自然是极好的啦~

每次获胜的时候,将数据存储一下并将临时数据清空一下就好啦。

很坑的是如果取“”这样的话,可能出现0:0,本题0:0也要记录……我不知道但因此WA了……

第一次提交——WA

在这里插入图片描述

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {

    private static boolean judgeEnding(int tempPointW, int tempPointL, int endCondition) {
        if ((tempPointW-tempPointL >= 2 && tempPointW >= endCondition) ||
                (tempPointL-tempPointW >= 2 && tempPointL >= endCondition)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> infoList = new LinkedList<>(), resultList = new LinkedList<>();
        boolean flag = false;
        while (!flag) {
            String line = scanner.nextLine();
            int index = line.indexOf("E");
            if (index != -1) {
                line = line.substring(0, index);
                flag = true;
            }
            infoList.add(line);
        }
        int tempPointW = 0, tempPointL = 0;
        scanner.close();
        for (String s : infoList) {
            for (char c : s.toCharArray()) {
                if (c == 'W') {
                    tempPointW++;
                } else {        //'L'
                    tempPointL++;
                }
                //结束判断
                if (judgeEnding(tempPointW, tempPointL, 11)) {
                    resultList.add(tempPointW + ":" + tempPointL);
                    tempPointW = 0;
                    tempPointL = 0;
                }
            }
        }
        if (tempPointW != 0 || tempPointL != 0) {
            resultList.add(tempPointW + ":" + tempPointL);
            tempPointW = 0;
            tempPointL = 0;
        }
        resultList.add("");
        for (String s : infoList) {
            for (char c : s.toCharArray()) {
                if (c == 'W') {
                    tempPointW++;
                } else {        //'L'
                    tempPointL++;
                }
                //结束判断
                if (judgeEnding(tempPointW, tempPointL, 21)) {
                    resultList.add(tempPointW + ":" + tempPointL);
                    tempPointW = 0;
                    tempPointL = 0;
                }
            }
        }
        if (tempPointW != 0 || tempPointL != 0) {
            resultList.add(tempPointW + ":" + tempPointL);
        }
        for (String s : resultList) {
            System.out.println(s);
        }
    }

}

获取了测试数据1:
in
EWLWLWL
out
0:0

0:0

错因是空串没处理,我决定加一个特判。

第二次提交——WA

在这里插入图片描述

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {

    private static boolean judgeEnding(int tempPointW, int tempPointL, int endCondition) {
        if ((tempPointW-tempPointL >= 2 && tempPointW >= endCondition) ||
                (tempPointL-tempPointW >= 2 && tempPointL >= endCondition)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> infoList = new LinkedList<>(), resultList = new LinkedList<>();
        boolean flag = false;
        while (!flag) {
            String line = scanner.nextLine();
            int index = line.indexOf("E");
            if (index != -1) {
                line = line.substring(0, index);
                flag = true;
            }
            infoList.add(line);
        }
        if (infoList.size() == 1 && "".equals(infoList.get(0))) {
            System.out.println("0:0\n\n0:0");
        }
        int tempPointW = 0, tempPointL = 0;
        scanner.close();
        for (String s : infoList) {
            for (char c : s.toCharArray()) {
                if (c == 'W') {
                    tempPointW++;
                } else {        //'L'
                    tempPointL++;
                }
                //结束判断
                if (judgeEnding(tempPointW, tempPointL, 11)) {
                    resultList.add(tempPointW + ":" + tempPointL);
                    tempPointW = 0;
                    tempPointL = 0;
                }
            }
        }
        if (tempPointW != 0 || tempPointL != 0) {
            resultList.add(tempPointW + ":" + tempPointL);
            tempPointW = 0;
            tempPointL = 0;
        }
        resultList.add("");
        for (String s : infoList) {
            for (char c : s.toCharArray()) {
                if (c == 'W') {
                    tempPointW++;
                } else {        //'L'
                    tempPointL++;
                }
                //结束判断
                if (judgeEnding(tempPointW, tempPointL, 21)) {
                    resultList.add(tempPointW + ":" + tempPointL);
                    tempPointW = 0;
                    tempPointL = 0;
                }
            }
        }
        if (tempPointW != 0 || tempPointL != 0) {
            resultList.add(tempPointW + ":" + tempPointL);
        }
        for (String s : resultList) {
            System.out.println(s);
        }
    }

}

获取了测试数据10:
in
WWWWWWWWWWWEadfadf;jadf
out
11:0
0:0

11:0

这样的话我觉得特判不是问题根源,而是应该在每次结束以后不加特判,就有了最后的代码。

第三次提交——AC

在这里插入图片描述

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {

    private static boolean judgeEnding(int tempPointW, int tempPointL, int endCondition) {
        if ((tempPointW-tempPointL >= 2 && tempPointW >= endCondition) ||
                (tempPointL-tempPointW >= 2 && tempPointL >= endCondition)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> infoList = new LinkedList<>(), resultList = new LinkedList<>();
        boolean flag = false;
        while (!flag) {
            String line = scanner.nextLine();
            int index = line.indexOf("E");
            if (index != -1) {
                line = line.substring(0, index);
                flag = true;
            }
            infoList.add(line);
        }
        int tempPointW = 0, tempPointL = 0;
        scanner.close();
        for (String s : infoList) {
            for (char c : s.toCharArray()) {
                if (c == 'W') {
                    tempPointW++;
                } else {        //'L'
                    tempPointL++;
                }
                //结束判断
                if (judgeEnding(tempPointW, tempPointL, 11)) {
                    resultList.add(tempPointW + ":" + tempPointL);
                    tempPointW = 0;
                    tempPointL = 0;
                }
            }
        }
        resultList.add(tempPointW + ":" + tempPointL);
        tempPointW = 0;
        tempPointL = 0;
        resultList.add("");
        for (String s : infoList) {
            for (char c : s.toCharArray()) {
                if (c == 'W') {
                    tempPointW++;
                } else {        //'L'
                    tempPointL++;
                }
                //结束判断
                if (judgeEnding(tempPointW, tempPointL, 21)) {
                    resultList.add(tempPointW + ":" + tempPointL);
                    tempPointW = 0;
                    tempPointL = 0;
                }
            }
        }
        resultList.add(tempPointW + ":" + tempPointL);
        for (String s : resultList) {
            System.out.println(s);
        }
    }

}
发布了357 篇原创文章 · 获赞 615 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43896318/article/details/104078684