184 条题解
-
1aph。 (chenqianrong) LV 10 @ 2021-09-04 14:14:35
#include <bits/stdc++.h> using namespace std; char s[110]; const int prime[25]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; int sum[26]; int main(){ int maxn=0,minn=110; cin>>s; int len=strlen(s); for(int i=0;i<len;i++) sum[s[i]-'a']++; for(int i=0;i<26;i++){ if(sum[i]>maxn) maxn=sum[i]; if(sum[i]>0&&sum[i]<minn) minn=sum[i]; } for(int i=0;i<25;i++){ if(maxn-minn==prime[i]){ printf("Lucky Word\n%d",maxn-minn); return 0; } } printf("No Answer\n0"); return 0; }
-
12021-05-02 21:16:58@
很烂的代码。。。
#include<bits/stdc++.h> using namespace std; int findma(int a[]){ int max=a[0]; for(int i=0;i<30;i++) if(max<a[i]) max=a[i]; return max; } int findmi(int a[]){ int min=1<<30; for(int i=0;i<30;i++) if(min>a[i]&&a[i]!=0) min=a[i]; return min; } bool isp(int n){ if(n==1||n==0) return false; if(n==2) return true; for(int i=2;i<=sqrt(n);i++) if(n%i==0) return false; return true; } int main(){ string s; getline(cin,s); int a[30]={0}; for(int i=0;i<s.size();i++) a[s[i]-97]++; int cntmax=findma(a),cntmin=findmi(a); if(isp(cntmax-cntmin)){ cout<<"Lucky Word"<<"\n"<<cntmax-cntmin; } else{ cout<<"No Answer"<<"\n"<<'0'; } return 0; }
-
12018-04-23 00:03:43@
一年前做过的题今天重做WA了数次!(捂脸)好吧,我是想再用线性素数筛做一下……
代码很简单,不再解释什么,但是一定要注意特判0不是素数!!!我就因为这个WA的……#include<iostream> #include<cstring> using namespace std; int maxn=0,minl=105,ans; string a; int js[150]; bool isprime[105]; void getlist(int n) { memset(isprime,1,sizeof(isprime)); isprime[0]=0; isprime[1]=0; for(int i=2;i<=n;++i) if(isprime[i]) for(int j=2;i*j<=n;++j) { isprime[i*j]=0; if(!i%j) break; } } int main() { cin>>a; for(int i=0;i<=a.length()-1;++i) js[a[i]]++; for(int i=97;i<=122;++i) { if(!js[i]) continue; if(js[i]>maxn) maxn=js[i]; if(js[i]<minl) minl=js[i]; } ans=maxn-minl; getlist(ans); if(isprime[ans]) cout<<"Lucky Word"<<endl<<ans; else cout<<"No Answer"<<endl<<0; return 0; }
-
12017-09-20 23:04:16@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;int trag,ans,i,minn,maxx,b[26];
int sushu[25]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
int main()
{
string A;
cin>>A;
int len=A.length();
for(i=0;i<len;i++)
{
b[A[i]-'a'+1]++;
}
std::sort(b+1,b+27);
for(i=1;i<=26;i++)
{
if(b[i]!=0)
{
minn=b[i];
break;
}}
maxx=b[26];
ans=maxx-minn;for(i=0;i<=24;i++)
{
if(ans==sushu[i])
{
trag=1;
break;
}
}if(trag==1)cout<<"Lucky Word"<<endl<<ans<<endl;
else cout<<"No Answer"<<endl<<"0"<<endl;
return 0;
} -
12017-03-07 20:09:09@
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
int h[1000];
char s[1000];
int main()
{
int x,k,i,j,maxn=0,minn=1000,n,sum,q=1;
scanf("%s",s);
n=strlen(s);
for(i=0;i<n;i++)
{
h[s[i]-96]++;
}
for(i=1;i<=26;i++)
{
if(h[i]>maxn)
maxn=h[i];
if(h[i]<minn&&h[i])
minn=h[i];
}
k=maxn-minn;
sum=0;
do
{
if(k==0)
break;
q++;
if(k%q==0)
sum=sum+1;}while(q<k);
if(sum==1)
{
printf("Lucky Word");
printf("\n%d",k);
}
else
{
printf("No Answer");
printf("\n%d",0);
}
system("pause");
return 0;
}c++标准写法
-
02020-09-18 16:23:00@
不算难题,注意大小写字符和质数判断就可以。
质数判断可以打表也可以开方逐个对比。题目给了长度在100以内,做个100内质数表也可以。#include <iostream> #include <string> #include <cmath> using namespace std; int acc[26]={}; int alpha(char c) { if(c>='A'&&c<='Z') { return c-'A'; } else { return c-'a'; } } bool isP(int a) { if(a<=1) { return false; } int s=sqrt(a); int i; for(i=2;i<=s;i++) { if(a%s==0) { return false; } } return true; } int main() { string str; cin>>str; int i,len=str.length(); for(i=0;i<len;i++) { acc[alpha(str[i])]++; } int ma=0,mi=10000; for(i=0;i<26;i++) { if(acc[i]>ma) { ma=acc[i]; } if(acc[i]<mi&&acc[i]!=0) { mi=acc[i]; } } if(isP(ma-mi)) { cout<<"Lucky Word"<<endl<<ma-mi<<endl; } else { cout<<"No Answer"<<endl<<0<<endl; } return 0; }
-
02019-08-07 20:11:03@
#include <bits/stdc++.h> using namespace std; bool prime[101]={false,false,true,true,false,true,false,true,false,false,false,true,false,true,false,false,false,true,false,true,false,false,false,true,false,false,false,false,false,true,false,true,false,false,false,false,false,true,false,false,false,true,false,true,false,false,false,true,false,false,false,false,false,true,false,false,false,false,false,true,false,true,false,false,false,false,false,true,false,false,false,true,false,true,false,false,false,false,false,true,false,false,false,true,false,false,false,false,false,true,false,false,false,false,false,false,false,true,false,false,false}; /* bool judge(int x) { if(x<2) return false; for(int i=2;i<x;i++) { if(x%i==0) return false; } return true; } int main () { for(int i=0;i<101;i++) { if(judge(i)) prime[i]=true; else prime[i]=false; } for(int i=0;i<101;i++) { if(prime[i]) cout<<"true,"; else cout<<"false,"; } return 0; }*/ int main () { string s; cin>>s; int word[26]={0}; for(int i=0;i<s.size();i++) { word[(int)(s[i]-'a')]++; } int Min=0x7fffffff,Max=0; for(int i=0;i<26;i++) { if(word[i]!=0) Min=min(Min,word[i]); Max=max(Max,word[i]); } if(prime[Max-Min]) { cout<<"Lucky Word\n"; cout<<Max-Min; } else { cout<<"No Answer \n0"; } return 0; }
-
02019-07-25 10:05:49@
因为一些字母没有出现,所以判断最少次数时记住要判断它不等于0
然后1和0不是质数
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word;char x;int s[26],i,l=0,xn,nn,n;bool y=true;
cin>>word;
for(i=0;i<26;i++) s[i]=0;
for(i=0;i<sizeof(word);i++) x=word[i];
for(i=0;i<sizeof(word);i++)
{
x=word[i];
s[x-97]++;
}
xn=0;nn=100;
for(i=0;i<26;i++)
{
if(xn<s[i]) xn=s[i];else
if(nn>s[i]&&s[i]!=0) nn=s[i];
}
n=xn-nn;
if(n==0||n==1) y=false;
for(i=2;i<n;i++) if(n%i==0) y=false;
if(y) cout<<"Lucky Word"<<endl<<n;
else cout<<"No Answer"<<endl<<0;
return 0;
} -
02018-11-07 21:51:15@
刚才忘记sum==0的特殊情况了
#include<bits/stdc++.h>
using namespace std;
int main()
{
string x;
cin>>x;
int len=x.length();
int book[26]={0};
int a=0,b=5;
for(int i=0;i<len;i++)
book[x[i]-'a']++;
for(int j=1;j<26;j++)
{
a=max(a,book[j]);
if(book[j]!=0)
b=min(b,book[j]);
}
int sum=a-b;
if(sum==1||sum==0)
{
cout<<"No Answer"<<endl<<0;
return 0;
}
for(int i=2;i<sum;i++)
if(sum%i==0)
{
cout<<"No Answer"<<endl<<0;
return 0;
}
cout<<"Lucky Word"<<endl<<sum;
return 0;
} -
02018-07-23 12:26:36@
C++AC标程:
/*
此题思路分析:
(1)建立一个数组,用来统计字符的统计数量,初始化为0;
(2)用循环统计字符个数;
(3)找到最大值和最小值;
(4)用最大值-最小值,判断是否是质数;
(5)如果是->输出Lucky Word->输出最大值-最小值,
否则->输出No Answer->输出0.
*/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int search[110]={0};//(1)
bool prime(int x)//判断质数
{
if ((x==0)||(x==1)) return false;//不要忘了这一句
for (int i=2;i<=sqrt(x);i++)
if (x%i==0) return false;
return true;
}
int main()
{
string s;
getline(cin,s);
int len=s.size();
int maxn=0,minn=101;
for (int i=0;i<len;i++)//(2)
search[(int)s[i]]++;//注意:千万不能把下面一个for循环放在这个循环里
for (int i=0;i<len;i++)
{
if (search[(int)s[i]]>maxn) maxn=search[(int)s[i]];//(3)
if (search[(int)s[i]]<minn) minn=search[(int)s[i]];//(3)
}
if (prime(maxn-minn)==true)//(4)
cout<<"Lucky Word"<<endl<<maxn-minn<<endl;//(5)
else cout<<"No Answer"<<endl<<0<<endl;//(5)
return 0;
} -
02018-06-15 22:24:49@
弹出一大堆警告,还不是对了
Accepted
//警告从这里开始
/in/foo.cc: In function 'int main()':
/in/foo.cc:18:2: warning: 'char* gets(char*)' is deprecated [-Wdeprecated-declarations]
gets(a);
^~~~
In file included from /usr/include/c++/6/cstdio:42:0,
from /in/foo.cc:1:
/usr/include/stdio.h:640:14: note: declared here
extern char *gets (char __s) __wur attribute_deprecated;
^~~~
/in/foo.cc:18:8: warning: 'char gets(char*)' is deprecated [-Wdeprecated-declarations]
gets(a);
^
In file included from /usr/include/c++/6/cstdio:42:0,
from /in/foo.cc:1:
/usr/include/stdio.h:640:14: note: declared here
extern char *gets (char *__s) __wur attribute_deprecated;
^~~~
/tmp/ccV5DQpE.o: In functionmain':
gets' function is dangerous and should not be used.
foo.cc:(.text.startup+0xc): warning: the
//到这里结束状态 耗时 内存占用
#1 Accepted 3ms 208.0 KiB
#2 Accepted 3ms 348.0 KiB
#3 Accepted 3ms 376.0 KiB
#4 Accepted 3ms 256.0 KiB
#5 Accepted 2ms 364.0 KiB
#6 Accepted 4ms 372.0 KiB
#7 Accepted 4ms 376.0 KiB
#8 Accepted 2ms 372.0 KiB
#9 Accepted 3ms 364.0 KiB
#10 Accepted 2ms 372.0 KiB
代码
#include<cstdio>
#include<cstring>
#include<cmath>
int zs(int x)
{
if(x==0||x==1)
return 0;
for(int i=2;i<=sqrt(x);i++)
if(x%i==0)
return 0;
return 1;
}
int b[105];
int main()
{
int i,max=0,min=100,s;
char a[105];
gets(a);
int lena=strlen(a);
for(i=0;i<lena;i++)
b[(int)a[i]-96]++;
for(i=1;i<=26;i++)
{
if(b[i]>0)
{
if(b[i]>max) max=b[i];
if(b[i]<min) min=b[i];
}
}
s=max-min;
if(zs(s)) printf("Lucky Word\n%d\n",s);
else printf("No Answer\n0\n");
return 0;
} -
02018-03-30 08:32:51@
#include <stdio.h>
#include <string.h>
int isitzhishu(int m)
{
int i;
if(m==0) return 0;
if(m==1) return 0;
if(m==2) return 1;
if(m==3) return 1;
for(i=2;i<m;i++)
{
if(m%i==0) return 0;
}
return 1;
}
int main()
{
char a[200];
int i,b[26],len,maxn=0,minn=100,m;
for(i=0;i<26;i++) b[i]=0;
scanf("%s",a);
len=strlen(a);
for(i=0;i<len;i++)
b[a[i]-'a']++;
for(i=0;i<26;i++)
{
// printf("%c: %d\n",i+'a',b[i]);
}
for(i=0;i<26;i++)
{
if(b[i]>maxn) maxn=b[i];
if(b[i]<minn&&b[i]!=0) minn=b[i];
}
m=maxn-minn;
// printf("max:%d\tmin:%d\tmax-min=%d\n",maxn,minn,m);if(isitzhishu(m))
printf("Lucky Word\n%d",m);
else
printf("No Answer\n0");}
-
02017-11-16 16:41:32@
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define FOR(i,x,y) for(i=x;i<=y;++i) using namespace std; int ss[30]; bool check(int x) { if(x==1||x==0) return false; int i; for(i=2;i*i<=x;++i) if(x%i==0) return false; return true; } int main() { char s[110]; scanf("%s",s); int len = strlen(s),i; FOR(i,0,len-1) ss[s[i]-'a']++; sort(ss,ss+26); int j; for(j=0;ss[j]==0&&j<26;++j); int t = ss[25]-ss[j]; if(check(t)) { cout<<"Lucky Word"<<endl; cout<<t<<endl; } else { cout<<"No Answer"<<endl; cout<<0<<endl; } }
-
02017-11-05 19:05:20@
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int j,a,i,n,k,maxn,minn=100,p;
char s[101];
int hesh[27];
int main()
{
// freopen("word10.in","r",stdin);
// freopen("word10.out","w",stdout);
scanf("%s",s);
n=strlen(s);
for(i=0;i<n;i++)
hesh[s[i]]++;
for(i=97;i<=122;i++)
{
if(hesh[i]!=0 && hesh[i]>maxn) maxn=hesh[i];
if(hesh[i]!=0 && hesh[i]<minn) minn=hesh[i];
}
k=maxn-minn;
for(i=1;i<=k;i++)
{
j=k%i;
if(j==0) a++;
}
if(a==2 && k!=0 && k!=1 )
printf("Lucky Word\n%d",k);
else
printf("No Answer\n%d",p);
return 0;
} -
02017-10-28 08:10:02@
桶
#include <queue> #include <cmath> #include <cctype> #include <vector> #include <cstdio> #include <cstring> #include <cstdlib> #include <iomanip> #include <iostream> #include <algorithm> using namespace std; char a[101]; int b[150]; int main() { int n,l=200,r=0,s; scanf("%s",a); n=strlen(a); for(int i=0;i<n;i++) { b[a[i]]++; } for(int i=97;i<=122;i++) { if(b[i]!=0&&b[i]<l) { l=b[i]; } r=max(r,b[i]); } s=r-l; if(s==0||s==1) { printf("No Answer\n0"); return 0; } else { for(int i=2;i<=sqrt(s);i++) { if(s%i==0) { printf("No Answer\n0"); return 0; } } printf("Lucky Word\n%d",s); } return 0; }
-
02017-10-02 13:47:45@
#include<iostream> #include<cstring> using namespace std; int word[27]; char n[105]; bool is_prime(int n){//判素,这个我认为要记住 if(n<2) return false; for(int i=2;i*i<=n;i++){ if(n%i==0) return false; } return true; } int main() { int max=0,min=99999999; gets(n);//特别注意这里不能用string,会出错 int len=strlen(n); for (int i=0;i<len;i++) word[n[i]-'a']++;//创建对应数组 for (int j=1;j<=25;j++){ if (word[j]>max) max=word[j]; if (word[j]!=0&&word[j]<min) min=word[j]; }//进行比较 int ans=max-min; if (is_prime(ans)) cout<<"Lucky Word"<<endl<<ans<<endl; else cout<<"No Answer"<<endl<<"0"<<endl;//判断输出 return 0; }*本菜第一次发表题解~~*
-
02017-09-20 22:46:38@
比较水的一道题
因为懒所以连O(n)的判素都没有写
当时我在纠结的是要是全是一个字母咋办。。。
```cpp
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
char c[110];
int num[28],su[110];
int main()
{
int i,shu,l,k,maxn=0,minn=110;
cin>>c;
for(i=2;i<=102;i++)
{
for(k=2;k<=floor(sqrt(i));k++)
if(i%k==0)
break;
if(k>floor(sqrt(i)))
su[i]=1;
}
shu=strlen(c);
for(i=0;i<shu;i++)
{
k=c[i]-'a'+1;
num[k]++;
}
for(l=1;l<=26;l++)
{
if(num[l]>maxn)
maxn=num[l];
if(num[l]<minn&&num[l]!=0)
minn=num[l];
}
l=maxn-minn;
if(su[l])
cout<<"Lucky Word"<<endl<<l;
else
cout<<"No Answer"<<endl<<"0";
return 0;}
``` -
02017-08-18 11:22:05@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
bool js(int o)
{
if(o==2) return true;
if(o==1||o==0) return false;
for(int y=2;y<=sqrt(o);++y)
{
if(o%y==0) return false;
}
return true;
}//质数模板
int main()
{
long int n[27],max=0,min=0,b,c=1;
char a[100];
cin>>a;
int sum=strlen(a);
for(int o=0;o<=26;++o)
{
n[o]=0;
}
for(int i=0;i<=sum-1;++i)
{
b=a[i]-96;//字符串转数字
n[b]++;
}
for(int j=1;j<=26;++j)
{
if(c==1&&n[j]>0)
{
c=0;
max=n[j];
min=n[j];
}
else
{
if(n[j]>max)
{
max=n[j];
}
if(n[j]<min&&n[j]>0)
{
min=n[j];
}
}
}
int mix=max-min;
if(js(mix)==true)
{
cout<<"Lucky Word"<<endl;
cout<<mix;
}
else
{
cout<<"No Answer"<<endl<<"0";
}
return 0;
} -
02017-03-10 20:12:26@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>using namespace std;
int main()
{
char d;
int c[26]={0};
for(int i=1;i<=101;i++)
{
d=(char)getchar();
if(d=='\n')
break;
else{
c[d-'a']++;
}
}
sort(c,c+26);
int max,min;
max=c[25];
for(int j=0;j<=25;j++)
{
if(c[j]!=0)
{
min=c[j];
break;
}
}
int p;
p=max-min;
if(p==2||p==3||p==5||p==7||p==11||p==13||p==17||p==19||p==23||p==29||p==31||p==37||p==41||p==43||p==47||p==53||p==50||p==61||p==67||p==71||p==73||p==79||p==83||p==87||p==89||p==91||p==97)
{
cout<<"Lucky Word"<<endl;
cout<<p;
}
else{
cout<<"No Answer"<<endl;
cout<<0;
}
return 0;
} -
02017-03-10 20:12:13@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>using namespace std;
int main()
{
char d;
int c[26]={0};
for(int i=1;i<=101;i++)
{
d=(char)getchar();
if(d=='\n')
break;
else{
c[d-'a']++;
}
}
sort(c,c+26);
int max,min;
max=c[25];
for(int j=0;j<=25;j++)
{
if(c[j]!=0)
{
min=c[j];
break;
}
}
int p;
p=max-min;
if(p==2||p==3||p==5||p==7||p==11||p==13||p==17||p==19||p==23||p==29||p==31||p==37||p==41||p==43||p==47||p==53||p==50||p==61||p==67||p==71||p==73||p==79||p==83||p==87||p==89||p==91||p==97)
{
cout<<"Lucky Word"<<endl;
cout<<p;
}
else{
cout<<"No Answer"<<endl;
cout<<0;
}
return 0;
}