zoukankan      html  css  js  c++  java
  • baidu c++吧上的一道题

    题目在:http://post.baidu.com/f?kz=70481398
    统计字母个数:
    程序输入一个字符串(长度不超过100),全是小写字母.
    统计小写字母出现的次数,并用要求的图表示出来.
    测试数据:

    Input:
    sadjhasdhqwpopeepomcxnnbladkjkfjasjas

    Output:
    @
    @                 @                 @
    @     @           @           @     @
    @     @ @     @   @ @     @ @ @     @
    @ @ @ @ @ @   @   @ @ @ @ @ @ @ @   @       @ @
    a b c d e f g h i j k l m n o p q r s t u v w x y z
     
    注意:
    在字符串中每个小写字母的个数不会超过20个.
    编程语言:C/C++.

    _________________________________________________
    题目意思就是每个字母有多少个,上面就输出多少@。我用一个长度为26的数组(数组int count[26])来保存每个字母的个数,然后用一个26*20(因为说了每个字母个数都不超过20个)的数组(数组chars)来记录最后输出的结果,这个数组中的刚开始初始化为空字符,每次碰到一个字母就将其赋值为@,最后只要把这个数组从最高的一排向下输出就得到结果了,但是上面好多排都可能是空字符,要从第一排含有至少一个非空字符(即@)的向下输出,所以我用了一个变量(int max_count)来记录这一排的位置。程序如下:

    #i nclude <iostream> 
    using namespace std;

    #define COUNT 20

    void output(char *p)
    {

        char chars[26][COUNT];
        for(int i=0;i<26;i++)
            for(int j=0;j<COUNT;j++)
                chars[i][j]
    =' ';

        int count[26];
        for(int i=0;i<26;i++)
            count[i]
    =0;

        int max_count=0;
        int index=0;
        int tmp=0;
        while(*p!='\0')
        {
            index
    =*p-'a';
            tmp
    =++count[index];
            if(max_count<tmp)
                max_count
    =tmp;
            chars[index][tmp
    -1]='@';
            p
    ++;
        }


        for(int i=max_count-1;i>=0;i--)
        {

            for(int j=0;j<26;j++)
                cout
    <<chars[j][i];
            cout
    <<endl;
        }

        for(char i='a';i<='z';i++)
            cout
    <<i;
    }


    int main()
    {

        char *str="sadjhasdhqwpopeepomcxnnbladkjkfjasjas",*p=str;
        output(p);
        getchar();

        return 0;
    }
  • 相关阅读:
    ubuntu修改主机名称+修改终端显示目录和计算机名称
    google 搜索 PPT
    就为在YouTube上下载个视频
    读书笔记 ---- 《嵌入式系统技术》
    WARNING: Unable to open an initial console
    Ubuntu ftp服务器搭建 + UltraEdit编辑FTP文件
    51Nod 1079 中国剩余定理【模板题】
    Leetcode 11 Container with most water【双指针】
    kuangbin专题六 最小生成树【从入门到熟练】【5题】
    kuangbin专题十二 基础DP1【从入门到熟练】【10题】
  • 原文地址:https://www.cnblogs.com/lzjsky/p/1936839.html
Copyright © 2011-2022 走看看