2017年5月月赛-暨中南大学暑期集训选拔赛第一场-部分题目

1.1924: 那些年寮里的崽儿们

  • 题目
    1924: 那些年寮里的崽儿们
  • 分析
    用Java来做的话,其实这道题可以用一个LinkedHashMap来保存输入的字符串和它出现的次数,因为LinkedHashMap遍历时的顺序就是插入时的顺序,所以正好满足题意。
  • 我的代码
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String,Integer> map = new LinkedHashMap<String,Integer>();
        int cas = 0;
        while(in.hasNext()){
            map.clear();
            int n = in.nextInt();
            in.nextLine();
            for(int i=0; i<n; i++){
                String str = in.nextLine();
                Integer times = map.get(str);
                if(times == null){
                    times = 0;
                }
                times++;
                map.put(str, times);
            }

            System.out.printf("Case %d:\n",++cas);
            Set<String> set = map.keySet();
            for(String str: set){
                System.out.printf("%s %d\n",str,map.get(str));
            }

        }

    }

}

2.

  • 题目
    1925: Apache的情书
  • 分析
    可以使用java中String的replaceAll()函数来做,但我不会进行正则表达式的有效匹配,所以只好写了16种情况。希望有知道如何可以用一个匹配写出来,请在评论里面告诉我,谢谢!
  • 我的代码
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        StringBuffer sb = new StringBuffer();
        String str;
        while(in.hasNext()){
            str =in.nextLine();
            str = str.replaceAll("love", "hate");
            str = str.replaceAll("lovE", "hatE");
            str = str.replaceAll("loVe", "haTe");
            str = str.replaceAll("loVE", "haTE");
            str = str.replaceAll("lOve", "hAte");
            str = str.replaceAll("lOvE", "hAtE");
            str = str.replaceAll("lOVe", "hATe");
            str = str.replaceAll("lOVE", "hATE");

            str = str.replaceAll("Love", "Hate");
            str = str.replaceAll("LovE", "HatE");
            str = str.replaceAll("LoVe", "HaTe");
            str = str.replaceAll("LoVE", "HaTE");
            str = str.replaceAll("LOve", "HAte");
            str = str.replaceAll("LOvE", "HAtE");
            str = str.replaceAll("LOVe", "HATe");
            str = str.replaceAll("LOVE", "HATE");

            System.out.println(str);
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值