算法提高 ADV-226 笨小猴
2017-01-16 18:50
239 查看
问题描述
笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼。但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大!
这种方法的具体描述如下:假设maxn是单词中出现次数最多的字母的出现次数,minn是单词中出现次数最少的字母的出现次数,如果maxn-minn是一个质数,那么笨小猴就认为这是个Lucky Word,这样的单词很可能就是正确的答案。
输入格式
输入文件只有一行,是一个单词,其中只可能出现小写字母,并且长度小于100。
输出格式
输出文件共两行,第一行是一个字符串,假设输入的的单词是Lucky Word,那么输出“Lucky Word”,否则输出“No Answer”;第二行是一个整数,如果输入单词是Lucky Word,输出maxn-minn的值,否则输出0。
样例输入
error
样例输出
Lucky Word
2
样例说明
单词error中出现最多的字母r出现了3次,出现次数最少的字母出现了1次,3-1=2,2是质数。
样例输入
olympic
样例输出
No Answer
0
样例说明
单词olympic中所有字母都只出现了1次,1-1=0,0不是质数。
笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼。但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大!
这种方法的具体描述如下:假设maxn是单词中出现次数最多的字母的出现次数,minn是单词中出现次数最少的字母的出现次数,如果maxn-minn是一个质数,那么笨小猴就认为这是个Lucky Word,这样的单词很可能就是正确的答案。
输入格式
输入文件只有一行,是一个单词,其中只可能出现小写字母,并且长度小于100。
输出格式
输出文件共两行,第一行是一个字符串,假设输入的的单词是Lucky Word,那么输出“Lucky Word”,否则输出“No Answer”;第二行是一个整数,如果输入单词是Lucky Word,输出maxn-minn的值,否则输出0。
样例输入
error
样例输出
Lucky Word
2
样例说明
单词error中出现最多的字母r出现了3次,出现次数最少的字母出现了1次,3-1=2,2是质数。
样例输入
olympic
样例输出
No Answer
0
样例说明
单词olympic中所有字母都只出现了1次,1-1=0,0不是质数。
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String words = scanner.nextLine(); words = words.replaceAll(" ", ""); Map<Character, Integer> alphabetCountMap = getCharMaps(words); Integer maxReduceMinAlphabetCount = reduceCount(alphabetCountMap); if(judgePrimeNumber(maxReduceMinAlphabetCount)){ System.out.println("Lucky Word"); System.out.println(maxReduceMinAlphabetCount.intValue()); } else { System.out.println("No Answer"); System.out.println(0); } } public static Map<Character, Integer> getCharMaps(String words){ Map<Character, Integer> alphabetCountMap = new HashMap<Character, Integer>(); for (int index = 0; index < words.length(); index++) { Character alphabet = words.charAt(index); Integer count = alphabetCountMap.get(alphabet); alphabetCoun a05b tMap.put(alphabet, count==null ? 1 : count+1); } return alphabetCountMap; } private static Integer reduceCount(Map<Character, Integer> alphabetCountMap) { Iterator<Entry<Character, Integer>> iterator = alphabetCountMap.entrySet().iterator(); Integer min = Integer.MAX_VALUE; Integer max = Integer.MIN_VALUE; while (iterator.hasNext()) { Entry<Character,Integer> entry = iterator.next(); if(min.intValue() > entry.getValue().intValue()){ min = entry.getValue(); } if(max.intValue() < entry.getValue().intValue()){ max = entry.getValue(); } } return max.intValue() - min.intValue(); } private static boolean judgePrimeNumber(Integer maxReduceMinAlphabetCount) { int maxReduceMinAlphaCountValue = maxReduceMinAlphabetCount.intValue(); if(maxReduceMinAlphaCountValue == 1 || maxReduceMinAlphaCountValue == 0){ return false; } for (int i = 2; i < (int)Math.sqrt(maxReduceMinAlphaCountValue)+1; i++) { if (maxReduceMinAlphaCountValue % i == 0){ return false; } } return true; } }
相关文章推荐
- 蓝桥杯 算法提高 9-3摩尔斯电码 ADV-226
- 蓝桥杯 ADV-226 算法提高 9-3摩尔斯电码
- 蓝桥杯 ADV-132 算法提高 笨小猴
- 算法提高 ADV-132 笨小猴
- 蓝桥 ADV-230 算法提高 12-1三角形 【数学公式】
- 蓝桥杯 ADV-166算法提高 聪明的美食家(java)
- ADV-69算法提高-质因数
- ADV-94-算法提高-复数归一化
- 蓝桥杯算法提高 ADV-197 大数乘法 题解
- 蓝桥杯 ADV-20 算法提高 交换Easy
- 蓝桥杯 ADV-82 算法提高 填充蛋糕
- 蓝桥杯 ADV-98 算法提高 约数个数
- 蓝桥杯 ADV-66算法提高 阮小二买彩票
- 蓝桥杯 ADV-193算法提高 盾神与条状项链
- 蓝桥杯 ADV-187 算法提高 勾股数
- 算法提高 ADV-207 最长字符序列
- 蓝桥杯练习系统试题集 算法提高 ADV-147 学霸的迷宫
- 算法提高 ADV-84 图形输出
- 算法提高 ADV-111 Quadratic Equation
- ADV-70-算法提高-冒泡法排序