baidu c++吧上的一道题

统计字母个数:
程序输入一个字符串(长度不超过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)来记录这一排的位置。程序如下:

#include < 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 ;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值