c/c++语言开发共享【题解】洛谷 P1125 笨小猴

洛谷 P1125 笨小猴题目链接思路水题用大小为26的数组存储每个字母出现的次数,最后排个序,把次数为0的字母去掉,用最大的次数减去最小的非零次数,判断质数就行。代码/* * @Description: * @Author: 多多 * @Date: 2020-10-24 13:23:05 * @LastEditTime: 2020-10-24 13:32:12 * @LastEditors: 多多 */#include <bits/stdc++.h>using na


洛谷 P1125 笨小猴

题目链接

思路

水题
用大小为26的数组存储每个字母出现的次数,最后排个序,把次数为0的字母去掉,用最大的次数减去最小的非零次数,判断质数就行。

代码

/*  * @Description:   * @Author: 多多  * @Date: 2020-10-24 13:23:05  * @LastEditTime: 2020-10-24 13:32:12  * @LastEditors: 多多  */ #include <bits/stdc++.h> using namespace std;  bool judge(int x) {     if (x < 2)     {         return false;     }     if (x == 2)     {         return true;     }     for (int i = 2; i <= floor(sqrt(x)); i++)     {         if (x % i == 0)         {             return false;         }     }     return true; }  int main() {     freopen64("./INPUT/P1125.in", "r", stdin);     ios::sync_with_stdio(false);     cin.tie(0);     string s;     cin >> s;     int a[26] = {0};     for (int i = 0; i < s.length(); i++)     {         a[s[i] - 'a']++;     }     sort(a, a + 26);     int i = 0;     while (a[i] == 0)     {         i++;     }     if (judge(a[25] - a[i]))     {         cout << "Lucky Word" << endl;         cout << a[25] - a[i] << endl;     }     else     {         cout << "No Answer" << endl;         cout << 0 << endl;     }     return 0; } 

c/c++开发分享【题解】洛谷 P1125 笨小猴地址:https://blog.csdn.net/weixin_39117125/article/details/109259195

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/597041.html

(0)
上一篇 2021年5月8日
下一篇 2021年5月8日

精彩推荐