1 条题解

  • 0
    @ 2021-6-15 1:39:31

    C :

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	char a[101];int i,j,count,max=0,min=100,s,t;
    	scanf("%s",a);
    	getchar();
    	for(i=0;i<strlen(a);i++)
    	{
    		count=0;
    		for(j=0;j<strlen(a);j++)
    		{
    			if(a[i]==a[j])
    				count++;
    		}
    		if(count>max)
    			max=count;
    		if(count<min)
    			min=count;
    	}
    	t=max-min;s=0;
    	for(i=1;i<t;i++)
    	{
    		if(t%i==0)
    			s++;
    	}
    	if(s==1)
    		printf("Lucky Word\n%d\n",t);
    	else
    		printf("No Answer\n0\n");
    	return 0;
    }
    

    C++ :

    #include<stdio.h>
    #include<string.h>
    char a[110],b[26];
    int sushu(int t)
    {
    	int i;
    	if(t==0||t==1)
    		return 0;
    	if(t==2)
    		return 1;
    	for(i=2;i<t/2;i++)
    	{
    		if(t%2==0)
    			return 0;
    	}
    	return 1;
    }
    int main()
    {
    	int i,c[27],max,min,j;
    	while(scanf("%s",a)!=EOF)
    	{
    		memset(c,0,sizeof(c));
    		for(i=0;i<strlen(a);i++)
    		{
    			c[a[i] - 'a'+1]++;
    		}
    		max = -1;
    		min = 1000;
    		for(i=1;i<=26;i++)
    		{
    			if(c[i])
    			{
    				if(c[i] > max)
    					max = c[i];
    				if(c[i] < min)
    					min = c[i];
    			}
    			
    		}
    		if(sushu(max-min))
    		{
    			printf("Lucky Word\n");
    			printf("%d\n",max-min);
    		}
    		else
    		{
    			printf("No Answer\n");
    			printf("0\n");
    		}
    	}
    	return 0;
    }
    
    

    Java :

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    class Search{
    	public boolean ReSearch(int n){
    		int i;
    		for(i=2;i<Math.sqrt(n)+1;i++)
    			if(n%i == 0) return false;
    			return true;
    	}
    }
    public class Main {
    	public static void main(String args[]){
    			char [] a = new char [100];
    			String s;
    			int i =0,t,len = 0,max = 0,min = 1000;
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    			try {
    					s = br.readLine();
    					len = s.length();
    					a = s.toCharArray();
    					for(char c='a';c<='z';++c){
    						t = 0;
    						for(i = 0;i<len;i++)
    							if (a[i] == c)        ++t;
    							if(t>max && t!=0)     max = t;
    							if(t<min && t!=0)     min = t;
    					}
    						t = max - min;
    						Search find = new Search();
    						if(t == 0 || t == 1){
    							System.out.print("No Answer\n"+"0\n");
    						}
    						else if(find.ReSearch(t) || t == 2){
    							System.out.println("Lucky Word\n"+t+"\n");
    						}
    						else
    							System.out.print("No Answer\n"+"0\n");
    			} catch (IOException e) {
    				// TODO 自动生成的 catch 块
    				e.printStackTrace();
    			}
    	}
    }
    
    
    • 1