Problem Description
Description
Vocabulary stupid monkey is very small, so every time when English multiple-choice questions are a headache.
But he found a way, the test proved that in this way the chance to choose the option when selected for very large!
This method is specifically described as follows: Suppose MAXN largest number of occurrences is the number of letters in a word appear, Minn number of occurrences is the least number of occurrences of the letter word, if maxn-minn is a prime number, it is considered stupid monkey a Lucky Word, this word is probably the right answer.
Input
Input file only one line, is a word which may only appear in lowercase letters, and the length is less than 100.
Output
A total of two lines of output file:
The first line is a string, assuming that the input word is Lucky Word, then the output "Lucky Word", otherwise output "No Answer";
the second line is an integer, if the input word is Lucky Word the output maxn-minn value, otherwise output 0.
Sample Input
error
Sample Output
Lucky Word
2
Thinking problems
- 1 using a first length of the array 26, the number of each letter appears recorded
- 2 to find the maximum and minimum values from the array
- Analyzing maxn-minn 3 is prime
!! Precautions
- 0 and 1 is not a prime number
- "A" and "aabb" These two cases should special note
The complete code
#include <iostream>
#include <string>
using namespace std;
bool issuper(int num){
// !!!0和1都不是质数
if(num == 0){
return false;
}else if(num == 1){
return false;
}else{
for(int i=2;i<num/2;i++){
if(num%i==0){
return false;
}
}
}
return true;
}
int main()
{
string c;
int len;
int ll[26] = {0};
int maxn=-1,minn=105;
int judge;
c = "aaabbb";
cin >> c;
len = c.length();
// 创建一个次数表
for(int i=0;i<len;i++){
ll[c[i]-'a']++;
}
// 如果是只有一类字母的情况
// 遍历次数表找出最大最小值
for(int i=0;i<26;i++){
if(ll[i]==0) continue;
if(ll[i]>maxn){
maxn = ll[i];
}
if(ll[i]<minn){
minn = ll[i];
}
}
if(len == maxn) minn=0;
// 判断是不是质数
bool ans = issuper(maxn-minn);
if(!ans){
cout << "No Answer" << endl;
cout << 0 << endl;
}else{
cout << "Lucky Word" << endl;
cout << maxn-minn << endl;
}
return 0;
}