先放题目
本题较简单,我个人觉得本题关键在于统计每个字母出现的次数。用以下的代码来处理这件事应该算是比较方便的(详情请看下面的代码及注释,我觉得应该是比较好理解的)。
for (int i=0; i<len; i++)
{
b=ch[i];
cnt[b-'a']++; //遍历字符数组,使对应的计数器加1;如:若ch[i]=='a',则cnt[0]加1,以此类推
}
下面是AC代码:(如果有更好的方法欢迎大家提出来)
#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{int primer (int a); //判断质数函数声明char ch[100],b;int cnt[26]= {0}; //初始化计数器,记录每个字母出现的次数int maxn=0,minn=200;scanf("%s",ch);int len=strlen(ch);for (int i=0; i<len; i++){b=ch[i];cnt[b-'a']++; //遍历字符数组,使对应的计数器加1;如:若ch[i]=='a',则cnt[0]加1,以此类推}for (int i=0; i<26; i++){if (cnt[i]!=0){if (cnt[i]>maxn) //找出maxn和minnmaxn=cnt[i];if (cnt[i]<minn)minn=cnt[i];}}if (primer(maxn-minn)) //调用函数判断是不是质数{printf("Lucky Word\n");printf("%d",maxn-minn);}else{printf("No Answer\n");printf("0");}
}int primer (int a) //判断质数的函数
{int n=0,b=sqrt(a);if (a==0||a==1)return 0;else for (int i=2; i<=b; i++){if (a%i==0){return 0;break;}}return 1;
}
继续加油!!!