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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值