• 九度oj 题目1021:统计字符


    题目描述:
        统计一个给定字符串中指定的字符出现的次数。
    输入:
        测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到'#'时输入结束,相应的结果不要输出。
    输出:
        对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出:
        c0 n0
        c1 n1
        c2 n2
        ...
        其中ci是第1行中第i个字符,ni是ci出现的次数。
    样例输入:
    I
    THIS IS A TEST
    i ng
    this is a long test string
    #
    样例输出:
    I 2
    i 3
      5
    n 2
    g 2

    题目很简单,直接上代码
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <string>
     5 #define MAX 202
     6 char key[MAX];
     7 char toDeal[MAX];
     8 int ans[MAX];
     9 
    10 int main(int argc, char const *argv[])
    11 {
    12     gets(key);
    13     while(strcmp(key, "#") != 0) {
    14         gets(toDeal);
    15         for(int i = 0; i < strlen(key); i++) {
    16             ans[i] = 0;
    17         }
    18         for(int i = 0; i < strlen(toDeal); i++) {
    19             for(int j = 0; j < strlen(key); j++) {
    20                 if(toDeal[i] == key[j]) {
    21                     ans[j]++;
    22                     break;
    23                 }
    24             }
    25         }
    26         for(int i = 0; i < strlen(key); i++) {
    27             printf("%c %d
    ",key[i],ans[i]);
    28         }
    29         gets(key);
    30     }    
    31     return 0;
    32 }
  • 相关阅读:
    谷歌(google)广告尺寸大小列表
    D盘Program Files 文件夹里文件不显示,没隐藏。怎么才能显示出来?
    请问IOS中做一个手机网站的app壳复杂吗?
    zblog2.X 连不上数据库原因
    二叉查找树的实现与讲解(C++)
    记一次应用异常,处理过程
    C# RSA加密
    js对象 c#对象转换
    C# 微信消息模板 发送
    iis 虚拟目录 文件服务器
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5680458.html
Copyright © 2020-2023  润新知