• 一个笔试题


    华为面试题目

    char *str = "AbcABca";
    写出一个函数,查找出每个字符的个数,区分大小写,要求时间复杂度是n(提示用ASCⅡ码)
    我是怎么都没想出来,望高手赐教

     

    #include <iostream>
    using namespace std;
    #define MAX_PATH 256
    int main()
    {

        
    int arr[MAX_PATH]={0};
        
    char *s="AbcABca";
        
    for(int i=0;i<strlen(s);++i)
            arr[s[i]]
    ++;

        
    for(int i=0;i<256;++i)
        {
            
    if(arr[i])
                cout
    <<(char)i<<":"<<arr[i]<<" ";
        }
        cout
    <<endl;
    }
    #include <stdio.h>

    int main(int argc, char** argv)
    {
        
    char *str = "AbcABca";
        
    int count[256= {0};

        
    for (char *p=str; *p; p++)
        {
            count[
    *p]++;
        }

        
    // print
        for (int i=0; i<256; i++)
        {
            
    if (count[i] > 0)
            {
                printf(
    "The count of %c is: %d\n",i, count[i]);
            }
        }

        
    return 0;
    }
    #include <iostream>
    #include 
    <cstring>
    using namespace std;

    void count(const char *str)
    {
        
    static int count[26*2];

        memset(count,
    0,sizeof(count));

        
    for(;*str!=0;++str)
        {
            
    if('a'<=*str && *str<='z')
            {
                
    ++count[*str-'a'];
            }
            
    else
            {
                
    ++count[*str-'A'+26];
            }
        }

        
    //打印

        
    for(int i=0;i<25;++i)
        {
            
    if(count[i]!=0)
            {
                cout
    <<(char)('a'+i)<<":"<<count[i]<<endl;
            }

            
    if(count[26+i]!=0)
            {
                cout
    <<(char)('A'+i)<<":"<<count[26+i]<<endl;
            }
        }
    }

    int main()
    {
        
    char buffer[100];
        cin
    >>buffer;

        count(buffer);

        
    return 0;
    }
  • 相关阅读:
    Powerdesigner数据库建模--概念模型--ER图【转】
    oralce闪回
    DBA
    linux socket使用经验总结
    寒假学习笔记1:结构化程序设计
    寒假作业2:简化电梯设计elevator
    鹤发银丝映日月,丹心热血沃新花——忆三位良师
    走廊泼水节
    种树
    P2938 [USACO09FEB]股票市场Stock Market
  • 原文地址:https://www.cnblogs.com/feng801/p/1999867.html
Copyright © 2020-2023  润新知