- 笨小猴
- 2015-08-18 19:56:07 @
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <math.h>
using namespace std;
char s[110];
int a[30];
int is_prime(int n){
if(n==0) return 0;
for(int i=2;i*i<=n;i++) if(n%i==0) return 0;
return 1;
}
int cmp(int n,int m){
return n<m?1:0;
}
int main(){
memset(a,0,sizeof(a));
int i,n,m,maxn,minn;
int pos=0;
cin>>s;
n=strlen(s);
for(i=0;i<n;i++) a[int(s[i])-96]++;
sort(a+1,a+27,cmp);
for(i=1;i<=26;i++) if(a[i]!=0){minn=a[i];break;}
maxn=a[26];
// cout<<maxn<<" "<<minn<<endl;
if(is_prime(maxn-minn)) cout<<"Lucky Word"<<endl<<maxn-minn<<endl;
else cout<<"No Anwer"<<endl<<"0"<<endl;
//system("pause");
return 0;
}
只过了五个点,不知道为什么……
2 条评论
-
qq872191552 LV 9 @ 2015-09-01 22:15:06
我来试tab
include <iostream> include <algorithm> include <string.h> include <stdio.h> include <math.h>
using namespace std;
char s[110];
int a[30];
int is_prime(int n){
if(n==0) return 0;
for(int i=2;i*i<=n;i++) if(n%i==0) return 0;
return 1;
}
int cmp(int n,int m){
return n<m?1:0; } int main(){ memset(a,0,sizeof(a)); int i,n,m,maxn,minn; int pos=0; cin>>s;
n=strlen(s);
for(i=0;i<n;i++) a[int(s[i])-96]++;
sort(a+1,a+27,cmp);
for(i=1;i<=26;i++) if(a[i]!=0){minn=a[i];break;}
maxn=a[26];
// cout<<maxn<<" "<<minn<<endl;
if(is_prime(maxn-minn)) cout<<"Lucky Word"<<endl<<maxn-minn<<endl;
else cout<<"No Anwer"<<endl<<"0"<<endl;
//system("pause");
return 0;
} -
2015-08-18 20:56:08@
素数判断写错了,1应该是合数而不是质数
吐槽一句,这一题明明可以 O(n),你却用了排序让时间复杂度上升至 O(nlogn),有些多余
以后发代码在代码第一行前与最后一行后各加一个回车,然后选中代码块,按tab,效果如下:int isPrime(int x){
int i;
if(x==0 || x==1)
return 0;
for(i=2; i*i<=x; i++){
if(x%i == 0)
return 0;
}
return 1;
}否则代码看得人太辛苦
- 1