• ECNU 3048 单词出现次数


    ECNU 3048 单词出现次数

    链接

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

    题目

    单点时限: 2.0 sec

    内存限制: 256 MB

    给定字符串 s(长度范围为 1 - 100),t(长度范围为 1 - 10),计算 t 作为单词在 s 中出现的次数。单词指由一个或多个空格分隔的子字符串。

    输入格式
    第 1 行:一个整数 () 为问题数。

    接下来共 2*T 行,每 2 行对应一个问题的 s 和 t。s 中的单词以若干空格分隔,空格有可能出现在开头或末尾。

    输出格式
    对于每个问题,输出一行问题的编号(0 开始编号,格式:case #0: 等)。

    然后对应每个问题在一行中输出 t 作为单词在 s 中出现的次数。

    样例
    input
    3
    1
    1
    And and and and 3
    and
    And end end End 12 18
    END
    output
    case #0:
    1
    case #1:
    3
    case #2:
    0

    思路

    重点是用next和nextline进行输入,带空格用nextline,并且要用nextline消去换行符,之后就是遍历了,难度较小。

    代码

      public static void fun() {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
    
        for (int i = 0; i < n; i++) {
          String temp = sc.nextLine();
          String line = sc.nextLine();
          String need = sc.next();
          String[] que = line.split(" ");
          int count = 0;
          for (int j = 0; j < que.length; j++) {
            if (que[j].equals(need)) {
              count++;
            }
          }
          System.out.println("case #" + i + ":");
          System.out.println(count);
        }
    
      }
    
  • 相关阅读:
    leetcode : Valid Sudoku
    leetcode : Longest Increasing Subsequence
    leetcode : Search for a Range
    leetcode : Search Insert Position
    leetcode : next permutation
    leetcode : Implement strStr()
    leetcode : Remove Element
    框架:Spring MVC
    笔试:在线编程相关
    J2EE:关系(一对多、多对一、多对多关系)
  • 原文地址:https://www.cnblogs.com/blogxjc/p/14382792.html
Copyright © 2020-2023  润新知