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--;
                }
            }
    
        }
    
    }
  • 相关阅读:
    设置linux文件权限,使得同一用户组的可以在文件夹内自由增删文件(夹)
    opencv3.4.8编译opencv-contrib并使用sift
    安装anaconda简单教程
    Arch升级时断开链接恢复
    配置远程连接容器内镜像(映射容器内系统的22端口到宿主机上)
    t-SNE是什么?
    远程连接路由器下的主机
    windows下,本地PyCharm连接远程服务器显示图片
    卷积网络中,关于BatchNorm的训练与加载
    学习Keras
  • 原文地址:https://www.cnblogs.com/huangZ-H/p/10075765.html
Copyright © 2011-2022 走看看