题解

179 条题解

  • 2
    @ 2018-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;   
    }
    
  • 2
    @ 2017-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;
    }

  • 2
    @ 2017-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++标准写法

  • 1
    @ 2018-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;
    }

  • 1
    @ 2018-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 function main':
    foo.cc:(.text.startup+0xc): warning: the
    gets' function is dangerous and should not be used.
    //到这里结束

    状态 耗时 内存占用

    #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;
    }

  • 1
    @ 2018-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");

    }

  • 1
    @ 2017-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;
    }
    
  • 1
    @ 2017-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;
    }

  • 1
    @ 2016-11-12 23:24:04

    啰嗦一下……假设这个字符串中的字母都是相同的,那max是那个字母,min也是那个字母,还有,强调个细节,1和0不是质数。

    #include<iostream>
    #include<cstring>
    using namespace std;
    int word[27]={0},MAX= -1,MIN=106;
    char s[105];
    int work(int x)
    {
    if(x==1 || !x)
    return 0;
    for(int i=2;i<=x/2;i++)
    if(x%i==0)
    return 0;
    return 1;
    }
    int main()
    {
    cin>>s;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    word[s[i]-'a'+1]++;
    for(int i=1;i<=26;i++)
    {
    if(word[i]>MAX)
    MAX=word[i];
    if(word[i]<MIN && word[i])
    MIN=word[i];
    }
    if(work(MAX-MIN))
    cout<<"Lucky Word"<<endl<<MAX-MIN;
    else
    cout<<"No Answer"<<endl<<'0';
    return 0;
    }

  • 0
    @ 2018-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;
    }

  • 0
    @ 2017-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;
        }
    }
    
    
  • 0
    @ 2017-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;
    }

  • 0
    @ 2017-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;
    }*本菜第一次发表题解~~*
    
  • 0
    @ 2017-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;

    }
    ```

  • 0
    @ 2017-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;
    }

  • 0
    @ 2017-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;
    }

  • 0
    @ 2016-08-16 18:46:36

    var
    s:string;
    min,max,n,i,m:longint;
    flag:boolean;
    f:array['a'..'z'] of longint;
    ch:char;
    begin
    min:=1000000;
    readln(s);
    n:=length(s);
    for i:=1 to n do begin
    inc(f[s[i]]);
    end;
    for ch:='a' to 'z' do begin
    if f[ch]>0 then begin
    if f[ch]>max then max:=f[ch];
    if f[ch]<min then min:=f[ch];
    end;
    end;
    m:=max-min;
    flag:=true;
    if m=1 then flag:=false;
    if m=2 then begin
    flag:=true;
    end;
    if (m<>1) and (m<>2) then begin
    for i:=2 to trunc(sqrt(m)) do begin
    if m mod i=0 then flag:=false;
    end;
    end;
    if flag=false then begin
    writeln('No Answer');
    writeln('0');
    end;
    if flag=true then begin
    writeln('Lucky Word');
    writeln(m);
    end;
    end.

  • 0
    @ 2016-07-25 09:19:56

    水题尝试一发miller_rabin
    ``` c++
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <map>
    #include <cstdlib>
    #include <ctime>
    using namespace std;

    map<char, int> mp;
    char str[1005];

    int pow(int a, int b, int m)
    {
    if (b == 0) return 1%m;
    int p = pow(a, b>>1, m);
    p = p*p%m;
    if (b&1) p*=a;
    return p%m;
    }

    bool miller_rabin(int n)
    {
    if (n<=1) return false;
    for (int i = 1; i <= 10; i++) {
    int a = rand()%(n-1)+1;
    if (pow(a, n-1, n) != 1) return false;
    }
    return true;
    }

    int main()
    {
    gets(str);
    srand(time(0));
    size_t j = strlen(str);
    for (size_t i = 0; i < j; i++)
    mp[str[i]]++;
    char mn, mx;
    mn = mx = *str;
    for (map<char,int>::iterator i = mp.begin(); i != mp.end(); ++i) {
    if (i->second > mp[mx]) mx = i->first;
    if (i->second < mp[mn]) mn = i->first;
    }
    int ans = mp[mx]-mp[mn];
    if (miller_rabin(ans)) {
    puts("Lucky Word");
    cout << ans << endl;
    } else {
    puts("No Answer\n0");
    }
    return 0;
    }

  • 0
    @ 2016-07-17 21:08:08

    标准水题啊啊。。

  • 0
    @ 2016-03-18 07:43:51

    测试数据 #0: Accepted, time = 0 ms, mem = 560 KiB, score = 10

    测试数据 #1: Accepted, time = 0 ms, mem = 560 KiB, score = 10

    测试数据 #2: Accepted, time = 0 ms, mem = 556 KiB, score = 10

    测试数据 #3: Accepted, time = 0 ms, mem = 560 KiB, score = 10

    测试数据 #4: Accepted, time = 0 ms, mem = 560 KiB, score = 10

    测试数据 #5: Accepted, time = 0 ms, mem = 560 KiB, score = 10

    测试数据 #6: Accepted, time = 0 ms, mem = 560 KiB, score = 10

    测试数据 #7: Accepted, time = 15 ms, mem = 556 KiB, score = 10

    测试数据 #8: Accepted, time = 0 ms, mem = 556 KiB, score = 10

    测试数据 #9: Accepted, time = 0 ms, mem = 560 KiB, score = 10

    Accepted, time = 15 ms, mem = 560 KiB, score = 100

信息

ID
1495
难度
5
分类
模拟 点击显示
标签
递交数
9401
已通过
3404
通过率
36%
上传者