Luogu brush questions C++ language | P1125 Stupid little monkey

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


【Description】

Stupid little monkey has a very small vocabulary, so every time he does English multiple-choice questions, he has a headache. But he found a method, and it has been proved by experiments that the probability of choosing the right option is very high when using this method!

The specific description of this method is as follows: Suppose maxn is the number of occurrences of the letter that appears the most frequently in the word, and minn is the number of occurrences of the letter that appears the least frequently in the word. If maxn−minn is a prime number, then the stupid monkey thinks that this Is a Lucky Word, such a word is likely to be the correct answer.

【enter】

A word in which only lowercase letters are possible and the length is less than 100.

【Output】

There are two lines in total, the first line is a string, assuming the input word is Lucky Word, then output Lucky Word, otherwise output No Answer;

The second line is an integer, if the input word is Lucky Word, output the value of maxn−minn, otherwise output 0.

【Input example】

error

【Example of output】

Lucky Word 2

【Code Explanation】

#include <bits/stdc++.h>
using namespace std;
bool fun(int n) {
    if (n < 2) return false;
    for (int i=2; i<n; i++) {
        if (n%i==0) return false;
    }
    return true;
}

int main()
{
    string s;
    int maxn=0, minn=100, t, mark=0, counts[150]={0};
    cin >> s;
    //统计每个字母出现次数
    for (int i=0; i<s.length(); i++) {
        counts[s[i]]++;
    }
    //比大小找到最大次数和最小次数
    for (int i= 'a'; i<='z'; i++) {
        if (counts[i]>maxn) maxn = counts[i];
        if (counts[i]<minn && counts[i]!=0) minn = counts[i];
    }
    //得到一个值
    t = maxn - minn;
    //对t做质数判定
    if (fun(t)) {
        cout << "Lucky Word" << endl << t;
    } else {
        cout << "No Answer" << endl << 0;
    }
    return 0;
}

【operation result】

olympic
No Answer
0

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132707458