<p>求代码</p>

你不会知道开开有多喜欢字符串!开开每拿到一个字符串,就会一直琢磨其中的规律,今天他又拿到了一个字符串,他这次想要尝试知道,字符串中出现的所有字母中,出现最多的字母,和出现最少的字母之间出现次数的差值会有多少呢?他遇到了麻烦,聪明的你可以帮他解决这个问题吗?

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
typedef struct
    {string ch;
    int total;
    }sum;
bool cmp1(sum a,sum b){
    return a.total>b.total;
}
int main()
{
    string str;


    getline(cin,str);
    int len=str.length();
    sum s[50];
    for(int i=0;i<len;i++)
    {
        int count=0;
        for(int j=0;j<len;j++)
        {
            if(str[i]==str[j])
            {
                ++count;
            }

        }
        s[i].ch=str[i];
        s[i].total=count;
    }
    sort(s,s+len,cmp1);
    cout<<s[1].total-s[len-1].total<<endl;
    
    return 0;

}

img

这跟判断字符串和字符出现的次数实现一样。然后排序。第一个是次数最多,最多一个最少。