zoukankan      html  css  js  c++  java
  • 一个笔试题

    华为面试题目

    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;
    }
  • 相关阅读:
    cocos2d-x系列笔记技巧篇(2)---关于CREATE_FUNC宏的用法
    Cocos2d-x开源、跨平台的游戏引擎
    Asp.Net Core 文件上传处理
    Asp.Net Core获取当前上下文对象
    Asp.Net Core 视图整理(一)
    SVG渲染顺序及z轴显示问题(zIndex)
    JavaScript Screen对象
    Javascript 对象(object)合并
    SVG.Js事件示例,简单绑定拖动操作
    SVG 文字居中整理
  • 原文地址:https://www.cnblogs.com/feng801/p/1999867.html
Copyright © 2011-2022 走看看