求字符串中第k个多的字符

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/miclover_feng/article/details/81073223
//#include <iostream>
//#include<string>
#include<bits/stdc++.h>

using namespace std;

int main()
{
	string str = "acacabcbdbfbdbd";
	int count[256] = {0};
	int k=3;
	int index = 0;

	for(int i=0;i<str.size();i++)
		count[str[i]]++;
		
	for(int i=0;i<k;i++)
	{
		int tmp = 0;
		for(int j=0;j<str.size();j++)
		{
			if(tmp<count[str[j]])
			{
				tmp = count[str[j]];
				index = j;
			}			
		}
		count[str[index]] = 0;
	}
	cout<<str[index]<<endl;		
	return 0;
}

猜你喜欢

转载自blog.csdn.net/miclover_feng/article/details/81073223