[NOIP2008 Improvement Group] Stupid little monkey.
Title Description
Stupid Monkey has a very small vocabulary, so he has a headache every time he does an English multiple choice question. But he has found a way, which has been tested and proved to have a very high chance of choosing the right one when he uses this method to select the options!
This method is described as follows: suppose maxn is the number of occurrences of the letter with the most occurrences in the word, and minn is the number of occurrences of the letter with the fewest occurrences in the word, and if maxn-minn is a prime number, then Stupid Monkey assumes that it's a Lucky Word, and that such a word is likely to be the correct answer.
input format
A word in which only lowercase letters are possible and the length is less than 100.
output format
There are two lines, the first line is a string, suppose the input word is Lucky Word, then the output isLucky Word
Otherwise outputNo Answer
;
The second line is an integer if the input word isLucky Word
outputmaxn-minn
value, otherwise 0 is output.
Sample #1
Sample Input #1
error
Sample Output #1
Lucky Word
2
Sample #2
Sample Input #2
olympic
Sample Output #2
No Answer
0
draw attention to sth.
[Input/Output Sample 1 Explanation
wordserror
The most frequent letter r occurs 3 times, the least frequent letter occurs 1 time, 3-1 = 2, and 2 is a prime number.
[Input/Output Sample 2 Explanation
wordsolympic
The letter with the highest number of occurrences appears 1 time, the letter with the lowest number of occurrences appears 1 time, 1-1 = 0, and 0 is not a prime number.
(The original clerical error has been corrected here.)
noip2008 Improve the first question
A question about strings -
We can roughly divide this question into the following steps:
1. Input a string, and traverse the string, set up an array of Numbers (we can initialize the Numbes array to 0 for the time being), store the number of characters that appear in the string.
2. Iterate through the array, and find the smallest in the array, and the number of times the character has appeared in the string of the minimum minn and maximum maxn (minimum value of the judgment of a pit - that is, we can not just find out the smallest value in the array, while the minimum value must be >0, the minimum value must be >0, and the minimum value must be >0, the minimum value must be >0, the minimum value must be >0, the minimum value must be the minimum value.
(Indicates that this character appears in the input string)
3. Define a function to determine whether an integer is prime or not
4. Find out the minimum and maximum value, define diff variable = maxn-minn, determine whether the number is prime, if not prime, the first line output "No Answer", the second line output 0 can be.
If the number is prime, then the first line outputs "Lucky Word" and the second line outputs the value of the variable diff.
The specific code that can be compiled and run is as follows:
#include <iostream>;
#include <cmath>;
using namespace std.
// Determine if a number is prime
bool isPrime(int n) {
if (n <= 1) {
return false; }
}
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return false; }
}
}
return true; }
}
int main() {
// Input string
cin >> s;
cin >> s;; //Use the Numbers array to determine the number of times a letter appears in this string.
//Use the Numbers array to determine the number of times a letter appears in the string.
int Numbers[26] = { 0 };
for (int i = 0; i < (); i++) {
int n = (s[i] - 'a');
Numbers[n]++;
}
int maxn = 0, minn = 110.
for (int i = 0; i < 26; i++) {
if (Numbers[i] > maxn) maxn = Numbers[i];
// Be sure to note here that you can't directly compare the smallest one in the Numbers array, but the smallest one in the Numbers array, and its value can't be equal to 0 (i.e., this letter has appeared at least once in the string)
if (Numbers[i] > 0 && Numbers[i] < minn) minn = Numbers[i].
}
int diff = maxn - minn;
if (isPrime(diff)) {
cout << "Lucky Word" << endl;
cout << diff.
}
else {
cout << "No Answer" << endl;
cout << 0; }
}
endl; cout << 0; }
}
What I'm trying to show with this question is that it's not always the case that when traversing an array to find the minimum value, the only logical condition is the minimum value.
For example, in this question, not only do we need to find the minimum value, but this minimum value must not be 0. We must clearly determine our need, and then write the judgment logic according to this need