字符串中出現次數最多的字母和次數

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	cout<<"Enter the string:"<<endl;
	string s;
	cin>>s;
	int N = s.length();
	vector<int> n(N,1);
	for(int i = 0;i < N;i++)
	{
		for(int j = i + 1;j < N;j++)
		{
			if(s[i] == s[j])
				n[i] += 1;
		}
	}
	
	int n_max = n[0];
	for(int i = 1;i < N;i++)
	{
		if(n[i] > n_max)
			n_max = n[i];
	}

	for(int i = 0;i < N;i++)
	{
		if(n[i] == n_max)
		{
			cout<<"出現最多次數的字母:"<<s[i]<<";次數:"<<n[i]<<endl;
		}
	}	
	
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章