zoukankan      html  css  js  c++  java
  • 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--;
                }
            }
    
        }
    
    }
  • 相关阅读:
    MINA的session.close
    Maven构建灵活配置文件
    函数的凹凸性
    幂函数习题
    2017全国卷1文科第9题高考真题的解法
    指数函数习题
    三角形的四心的向量表示
    进退中体会数学运算和数学策略
    函数f(x+1)和f(x-1)的奇偶性
    函数的奇偶性周期性习题
  • 原文地址:https://www.cnblogs.com/huangZ-H/p/10075765.html
Copyright © 2011-2022 走看看