Uchiha_Itachi 的博客

Uchiha_Itachi 的博客

题解 P1125 【笨小猴】

posted on 2018-08-12 17:40:01 | under 题解 |

个人(因为太弱)觉得楼下几楼某巨佬写的map太高深,理解不了ToT,故决定自己写映射,然后就写出了一并不美观的代码。总的来说,题目很水,就是记录+判素,也不多说,上代码————

#include <bits/stdc++.h>
using namespace std;

int cti(char ch)
{
    return (int)ch - 96;
}//map from char to int

bool isprime(int n)
{
    int temp = ceil(sqrt(n));
    if(n == 2)
        return true;
    if(n == 1 || n == 0)
        return false;
    for(int i = 2;i <= temp;i++)
    {
        if(n % i == 0)
            return false;
    }
    return true;
} //prime judge

int appear[27];//each letter's appear time
char word[101];//word

int main()
{
    cin >> word;
    int sz = strlen(word);
    for(int i = 0;i < sz;i++)
    {
        appear[cti(word[i])]++;//process each letter
    }
    for(int i = 1;i <= 26;i++)
    {
        cout << char(i + 64) << ": " << appear[i] << endl;
    }
    sort(appear + 1,appear + 26);
    int minn = 0;
    for(int i = 1;i <= 26;i++)
    {
        if(appear[i])
        {
            minn = appear[i];//the first non-0 number is the smallest
            break;
        }
    }
    int maxn = -1;
    for(int i = 1;i <= 26;i++)
    {
        maxn = max(maxn,appear[i]);
    }
    cout << "Max: " << maxn << endl << "Min: " << minn << endl;
    if(isprime(maxn - minn))//prime
    {
        cout << "Lucky Word" << endl;
        cout << maxn - minn << endl;//as problem
    }
    else
    {
        cout << "No Answer" << endl;
        cout << 0 << endl;//as problem
    }
    return 0;
}

蒟蒻代码奉上:-)

又及:我是不会让你抄袭的![e]滑稽[e]