日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2025/3/15 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 根据特殊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语言描述)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。