zoukankan      html  css  js  c++  java
  • TJU 2468 Counting Letters

                          2468 Counting Letters 

    As a talented student, your boss gave you a task. Given a text string, you should find out which letters appear most frequently.

    Really simple, isn't it?

    Input

    The first line of the input is the number of test cases. Then some test cases followed.

    Each test case contains only one line, indicating the string mentioned above.

    You can assume there is only lower case letters in the string. The length of the string is more than 0 and no more than 100.

    Output

    You should output one line for each test case, indicating the most frequent letters. Please note there may be more than one such letter, so you should output all of them, and these letters should be in the same order as the alphabet, that is, 'a', 'b', 'c', 'd' ... 'z'.

    Sample Input

    2
    abcda
    tjucstju
    

    Sample Output

    a
    jtu
    

    Hint

    In the first sample test case, 'a' appears twice, while 'b','c','d' only appear once. So 'a' is the most frequent letter.

    In the second sample test case, 't', 'j' and 'u' appear twice, so you should output them all, and 'j' should be the first.

    Please note you should not output any unnecessary spaces or empty lines, or else you may get 'Presentation Error'.

    解题报告:好久没有在OJ上做题了,今天有人问我这道题怎么做,嘿嘿,就写了写,题意就是统计字母出现最多的,如果有多个字母出现一样就按字母的前后顺序输出,

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int MAX = 110;
    int main()
    {
        int n, i, max, len, num;
        char c[MAX];
        int count[30];
        scanf("%d", &n);
        while (n --)
        {
            memset(count, 0, sizeof(count));
            scanf("%s", c);
            len = strlen(c);
            for(i = 0; i < len; i ++)
            {
                num = c[i] - 'a';
                count[num]++;
            }
            max = 0;
            for (i = 0; i < 26; i ++)
            {
                if(max < count[i])
                {
                    max = count[i];
                }
            }
            for (i = 0; i < 26; i ++)
            {
                if (max == count[i])
                {
                    printf("%c", i + 'a');
                }
            }
            printf("\n");
        }
        return 0;
    }
  • 相关阅读:
    固定表头/锁定前几列的代码参考[JS篇]
    盘点mysql中容易被我们误会的地方
    cookie&session的Q&A故事[原理篇]
    网络第一道防线:验证码的故事[安全篇]
    2016,把一年的牛皮先吹了吧[生涯规划篇]
    微软职位内部推荐-Software Engineer II
    微软职位内部推荐-Senior Software Engineer
    微软职位内部推荐-Senior Software Engineer
    微软职位内部推荐-SW Engineer II for Cloud Servi
    微软职位内部推荐-SW Engineer II for Cloud Servi
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2659137.html
Copyright © 2011-2022 走看看