P1125 [NOIP 2008 提高组] 笨小猴
思路:
好吧,这道题目简洁明了,读入一个字符串,用 储存每个字母出现的次数,然后取最大值和除了 0 以外的最小值,最后检查 是不是质数即可。
代码:
#include<bits/stdc++.h>
using namespace std;
int a[26];//储存字母出现次数
bool iszhi(int t){//判断质数
if(t < 2) return false;
for(int i = 2;i*i<=t;i++){
if(t % i == 0) return false;
}
return true;
}
int main(){
string s;
cin>>s;
memset(a,0,sizeof(a));
for(int i = 0;i<s.size();i++){
a[s[i] - 'a']++;
}
int maxn = -1e9 ,minn = 1e9;
for(int i = 0;i<=25;i++){
if(a[i] != 0) {//取非零的数比较
maxn = max(maxn,a[i]);
minn = min(minn,a[i]);
}
}
if(iszhi(maxn - minn)){//判断是不是质数
cout<<"Lucky Word"<<endl;
cout<<maxn - minn;
}
else{
cout<<"No Answer"<<endl;
cout<<0;
}
return 0;
}