• 杭电oj1219 AC Me


    Tips:本题中,输入字符串之后,直接从头到尾处理一遍,调用函数判断是否是字母,不要自己写循环判断是否为字母,易超时!

    不过本题中有一个疑问,自己最开始用C写的,一直是Time Limit Exceeded ,但使用C++写之后就AC了

     1 #include <iostream>
     2 #include <string>
     3 #include <string.h>
     4 #include <ctype.h>
     5 using namespace std;
     6 int result[26];
     7 int main()
     8 {
     9     string str;
    10     while(getline(cin , str))
    11     {
    12         memset(result , 0 , sizeof result);
    13         for(int i=0; i<str.length();++i)
    14         {
    15             if(isalpha(str[i]))
    16             {
    17                 result[str[i]-'a']++;
    18             }
    19         }
    20         for(int i=0;i<26;++i)
    21         {
    22             cout << (char)('a'+i) << ":" <<result[i] << endl;
    23         }
    24         cout << endl;
    25     }
    26     return 0;
    27 }

    下面是无法AC的程序

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<ctype.h>
     4 int main()
     5 {
     6     char s[100001];
     7     int a[26];
     8     while(gets(s))
     9     {
    10         memset(a,0,sizeof(a));
    11         int i,j;
    12         for(i = 0;i < strlen(s);i++)
    13         {
    14             if(s[i]>='a'&&s[i]<='z')
    15             {
    16                 a[s[i]-'a']++;
    17             }
    18         }
    19         for(j = 0;j < 26;j++)
    20         {
    21             printf("%c:%d
    ",j+'a',a[j]);
    22         }
    23         printf("
    ");
    24     }
    25     return 0;
    26 }
  • 相关阅读:
    关于 Profile
    empty
    Vim Editor
    C++ Note
    Android NDK Sample
    Dealing with the ! when you import android project
    File attributes and Authority of Linux
    Java与C的相互调用
    The source code list of Android Git project
    Enable Android progurad in the case of multiple JAR lib
  • 原文地址:https://www.cnblogs.com/wujiyang/p/4541629.html
Copyright © 2020-2023  润新知