java函数版本
查看原帖
java函数版本
1498670
huicheng楼主2025/6/14 23:14

java函数版本 1,题目要读清楚 注意那个大于2分的条件 2,注意字符的读取,我只想到了一个字符一个字符的读取,因为当只输入一行的时候,如果按照常规的行读,会导致必须要再读一个换行

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        StringBuilder s = new StringBuilder();
        int input;
        while ((input = System.in.read()) != -1) { // 读取字节流
            char c = (char) input;
            s.append(c);
            if (c == 'E') break; // 检测到 'E' 退出
        }
        String str=s.toString();

        score(str,11);
        System.out.println();
        score(str,21);


    }
    public static void score(String str,int rule) {
        int W=0;
        int L=0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == 'W') {
                W++;
                if(Math.max(W,L)>=rule && Math.abs(W-L)>=2){
                    System.out.println(W+":"+L);
                    W=0; L=0;
                }
            }
            else if (str.charAt(i) == 'L') {
                L++;
                if(Math.max(W,L)>=rule && Math.abs(W-L)>=2){
                    System.out.println(W+":"+L);
                    W=0;  L=0;
                }
            }
            else if (str.charAt(i) == 'E') {
                System.out.println(W+":"+L);
                break;
            }
        }
    }

}
2025/6/14 23:14
加载中...