0%

题解P1125[笨小猴]

题目来源P1125 https://www.luogu.com.cn/problemnew/show/P1125

头文件$algorithm$中有函数count()

查询MSDN得:它的效果是取得一个范围中指定元素的多少,使用方法和sort()类似

枚举每一个字符,判一下质数,再求count值即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;

string word;
int maxn = -1, minn = 101;

void GetMaxMin(string);
bool prime(int);

int main()
{
cin >> word;
GetMaxMin(word);
if (prime(maxn - minn))
cout << "Lucky Word" << endl << maxn - minn;
else
cout << "No Answer" << endl << 0;
return 0;
}
void GetMaxMin(string s)
{
for (char c = 'a'; c <= 'z'; c++)
{
int t = count(&s[0], &s[s.length()], c);
maxn = max(t, maxn);
if (t != 0)
minn = min(t, minn);
}
}
bool prime(int x)
{
if (x < 2)
return false;
for (int i = 2; i * i <= x; i++)
if (x % i == 0)
return false;
return true;
}