2018/12/06 L1-023 输出GPLT Java

首先用switch实现了一个方案, 但是有两个点不能通过, 上代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main2 {

    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine().toUpperCase();
        // System.out.println(str);
        int G_len = 0;
        int P_len = 0;
        int L_len = 0;
        int T_len = 0;
        
        for(int i=0; i<str.length(); i++) {
            // 使用if...else if...else...方案
            // 提交结果出来了, 使用这种方案比switch方案快 
            if(str.charAt(i) == 'G') {
                G_len++;
            } else if (str.charAt(i) == 'P') {
                P_len++;
            } else if (str.charAt(i) == 'L') {
                L_len++;
            } else if (str.charAt(i) == 'T') {
                T_len++;
            } else {
                continue;
            }
        }
        
        while (G_len > 0 || P_len > 0 || L_len > 0 || T_len > 0) {
            if (G_len > 0) {
                System.out.print('G');
                G_len--;
            }
            if (P_len > 0) {
                System.out.print('P');
                P_len--;
            }
            if (L_len > 0) {
                System.out.print('L');
                L_len--;
            }
            if (T_len > 0) {
                System.out.print('T');
                T_len--;
            }
        }

    }

}

后该用if...else if...else替代switch, 成功通过了检测, 速度大大提高, 一下为代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main2 {

    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine().toUpperCase();
        // System.out.println(str);
        int G_len = 0;
        int P_len = 0;
        int L_len = 0;
        int T_len = 0;
        
        for(int i=0; i<str.length(); i++) {
            // 使用if...else if...else...方案
            // 提交结果出来了, 使用这种方案比switch方案快 
            if(str.charAt(i) == 'G') {
                G_len++;
            } else if (str.charAt(i) == 'P') {
                P_len++;
            } else if (str.charAt(i) == 'L') {
                L_len++;
            } else if (str.charAt(i) == 'T') {
                T_len++;
            } else {
                continue;
            }
        }
        
        while (G_len > 0 || P_len > 0 || L_len > 0 || T_len > 0) {
            if (G_len > 0) {
                System.out.print('G');
                G_len--;
            }
            if (P_len > 0) {
                System.out.print('P');
                P_len--;
            }
            if (L_len > 0) {
                System.out.print('L');
                L_len--;
            }
            if (T_len > 0) {
                System.out.print('T');
                T_len--;
            }
        }

    }

}
原文地址:https://www.cnblogs.com/huangZ-H/p/10075765.html