沐里纷纷


私信TA

用户名:Epoch

访问量:63702

签 名:

我不会算法

等  级
排  名 37
经  验 12910
参赛次数 1
文章发表 172
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

不会算法

解题思路:

核心就是字母哈希,练一下stl模板的map是可以的

注意事项:

C++是世界上最好的语言

参考代码:

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <stdio.h>

using namespace std;

map<char,int> letterCount;

bool isPrime(const int n)
{
	bool ret = true;
	if (n == 1)
		return false;
	else if (n == 0)
		return false;
	else
	{
		for (int i = 2; i*i <= n; i++)
			if (n%i == 0)
				ret = false;
	}
	return ret;
}

void getLetterCount(string& s)
{
	for (string::iterator it = s.begin(); it < s.end(); it++)
		letterCount[*it]++;
}

void getMinMax(int& min, int& max)
{
	max = 1;
	//对于map的迭代器,要使用!=运算符,<不能再使用
	for (map<char, int>::iterator it = letterCount.begin(); it != letterCount.end(); it++)
		if (it->second > max)
			max = it->second;
	min = max;
	for (map<char, int>::iterator it = letterCount.begin(); it != letterCount.end(); it++)
		if (it->second < min && it->second >= 1)
			min = it->second;	
}

int main(int argc, char** argv)
{
	string word;
	cin >> word;	
	getLetterCount(word);
	int min = 0, max = 0;
	getMinMax(min, max);
	if (isPrime(max - min))
		cout << "Lucky Word" << "\n" << max - min;
	else
		cout << "No Answer" << "\n" << 0;
	system("pause");
	return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区