zoukankan      html  css  js  c++  java
  • 统计字符

    Problem Description
    统计一个给定字符串中指定的字符出现的次数
     
    Input
    测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到'#'时输入结束,相应的结果不要输出。
     
    Output
    对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出:
    c0 n0
    c1 n1
    c2 n2
    ... 
    其中ci是第1行中第i个字符,ni是ci出现的次数。
     
    Sample Input
    I
    THIS IS A TEST
    i ng
    this is a long test string
    #
     
    Sample Output
    I 2
    i 3
      5
    n 2
    g 2
    注:第2个测试用例中,空格也是被统计的字符之一。
     
     1 #include <stdio.h> 
     2 #include <string.h>
     3 
     4 int main(){
     5     char s1[6];
     6     char s2[81];
     7     int s1_length;
     8     int s2_length;
     9     int i;
    10     int j;
    11     int time;
    12     char c;
    13 
    14     while(1){
    15         i=0;    //s1包含空格
    16         while((c=getchar())!='
    '){
    17             s1[i]=c;
    18             i++;
    19         }
    20         s1[i]='';
    21 
    22         if(s1[0]=='#')    
    23             break;
    24 
    25         i=0;    //s2包含空格
    26         while((c=getchar())!='
    '){
    27             s2[i]=c;
    28             i++;
    29         }
    30         s2[i]='';
    31 
    32         s1_length=strlen(s1);
    33         s2_length=strlen(s2);
    34 
    35         for(i=0;i<s1_length;i++){
    36             time=0;
    37             for(j=0;j<s2_length;j++){
    38                 if(s1[i]==s2[j])
    39                     time++;
    40             }
    41 
    42             printf("%c %d
    ",s1[i],time);
    43         }
    44     }
    45     return 0;
    46 }
     
     
     
     
     
     
     
  • 相关阅读:
    POJ 2342.Anniversary party-树形dp
    Codeforces Round #363 (Div. 2) A、B、C
    Codeforces Beta Round #17 D.Notepad 指数循环节
    hdu 5920 Wool 思路
    hdu 5719 Arrange 贪心
    hdu 5718 Oracle 高精度
    hiho #1332 : 简单计算器 栈+递归
    UESTC 1074 秋实大哥搞算数 栈模拟
    cdoj 1329 卿学姐与魔法 优先队列
    cdoj 1324 卿学姐与公主 线段树裸题
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/4066591.html
Copyright © 2011-2022 走看看