P1125 笨小猴

xiaoxiao2021-02-27  158

题目描述

笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼。但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大!

这种方法的具体描述如下:假设maxn是单词中出现次数最多的字母的出现次数,minn是单词中出现次数最少的字母的出现次数,如果maxn-minn是一个质数,那么笨小猴就认为这是个Lucky Word,这样的单词很可能就是正确的答案。

输入输出格式

输入格式: 输入文件word.in只有一行,是一个单词,其中只可能出现小写字母,并且长度小于100。

输出格式: 输出文件word.out共两行,第一行是一个字符串,假设输入的的单词是Lucky Word,那么输出“Lucky Word”,否则输出“No Answer”;

第二行是一个整数,如果输入单词是Lucky Word,输出maxn-minn的值,否则输出0。

输入输出样例

输入样例#1: error 输出样例#1: Lucky Word 2 输入样例#2: olympic 输出样例#2: No Answer 0 说明

【输入输出样例1解释】

单词error中出现最多的字母r出现了3次,出现次数最少的字母出现了1次,3-1=2,2是质数。

【输入输出样例2解释】

单词olympic中出现最多的字母i出现了2次,出现次数最少的字母出现了1次,2-1=1,1不是质数。

//注:此处原题解释有误,实际上是0,也不是质数。

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstdio> using namespace std; inline int read(){ char c; int res,flag = 0; while((c = getchar())>'9'||c<'0') if(c=='-')flag = 1; res = c - '0'; while((c = getchar())>='0'&&c<='9') res =(res<<3)+(res<<1) + c - '0'; return flag?-res:res; } int zhishu[1000]; int shuai(int h){ memset(zhishu,false,sizeof(zhishu)); zhishu[1] = true; zhishu[0] = true; for (int i =2;i<=h;i++){ int j = 2; while(i * j < h){ zhishu[i * j] = true; j++; } } } char ch; int count[27]; int maxx = 0,minn = 10000; int main(){ shuai(1000); memset(count,-1,sizeof(count)); while ((ch=getchar())>='a'&&ch<='z'){ // cout<<ch<<" "; if (count[ch - 97]== -1) count[ch - 97] = 1; else count[ch-97]++; } for (int i=0;i<26;i++){ if (count[i] != -1){ maxx = max(maxx,count[i]); minn = min(minn,count[i]); } } // cout<<maxx<<" "<<minn; if (!zhishu[maxx - minn]) cout<<"Lucky Word"<<endl<<maxx - minn; else cout<<"No Answer"<<endl<<"0"; }

先打一个筛法求素数,然后边读入数据边处理,找最大值最小值

转载请注明原文地址: https://www.6miu.com/read-12096.html

最新回复(0)