• ECNU 2974 统计单词个数


    ECNU 2974 统计单词个数

    链接

    https://acm.ecnu.edu.cn/problem/2974

    题目

    单点时限: 2.0 sec

    内存限制: 256 MB

    给定一行由若干英文单词和空格组成的字符串,统计该字符串中有效单词的个数,单词与单词之间用一个空格来分隔,其中的 the,a,an,of,for 及 and 被忽略,不作统计。

    输入格式
    第 行:整数 为问题数。

    第 ∽ 行:每一个问题中一行由若干英文单词和空格组成的字符串,字符串的长度不超过 个字符。

    输出格式
    对于每个问题,输出一行问题的编号( 开始编号,格式:case #0: 等),然后在新的一行中输出统计得到的单词个数,行末尾输出一个换行符。

    样例
    input
    2
    This is a sample
    Love is a lamp while friendship is the shadow
    output
    case #0:
    3
    case #1:
    7

    思路

    题目不是很难,但是陷阱不少。
    首先对于输入,就要考虑到接受空格,所以要用一个nextline把前面的换行符消去。之后输入存在大写与小写,这里应该加一个改变,全改为小写,最后统计比较倒不是很麻烦。

    代码

      public static void fun() {
        Scanner sc = new Scanner(System.in);
    
        int n = sc.nextInt();
        String temp = sc.nextLine();
        String[] mod = new String[]{"the", "a", "an", "of", "for", "and"};
        for (int t = 0; t < n; t++) {
          String str = sc.nextLine();
          str = str.toLowerCase();
          String[] a = str.split(" ");
          int num = 0;
          for (String s : a) {
            int flag = 0;
            for (int i = 0; i < mod.length; i++) {
              if (s.equals(mod[i])) {
                flag++;
              }
            }
            if (flag == 0) {
              num++;
            }
          }
          System.out.println("case #" + t + ":");
          System.out.println(num);
        }
      }
    
    
  • 相关阅读:
    bfs,队列
    Wannafly挑战赛22 A计数器(裴蜀定理 gcd)
    素数筛模板
    HDU
    HDU
    控制精度-----直接截取,不需四舍五入
    jstl下载与配置
    B. Treasure Hunt
    动态数组vector
    Manacher算法
  • 原文地址:https://www.cnblogs.com/blogxjc/p/14332140.html
Copyright © 2020-2023  润新知