根据特殊EOF的序列判断比赛输赢(洛谷P1042题题解,Java语言描述)
生活随笔
收集整理的這篇文章主要介紹了
根据特殊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);}}}總結
以上是生活随笔為你收集整理的根据特殊EOF的序列判断比赛输赢(洛谷P1042题题解,Java语言描述)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java】深入探讨Java数值舍入问题
- 下一篇: 复试分数线该怎么划定呢(洛谷P1068题