跳至正文

NOIP 2008 笨小猴 解题报告

  这个题目很简单,不过我也提交了两次。。问题见注释。
#include <math.h>
#include <stdio.h>
#include <string.h>
char str[101];
int count[26];

int isprime(int n)
{
        int li;
        int i;
        if(n == 0 || n == 1){
                //0和1不算素数 
                return 0;
        }
        li = sqrt(n);
        for(i = 2; i <= li; i++){
                if(n % i == 0){
                        return 0;
                }
        }
        return 1;
}

int main(void)
{
        int i, len;
        int max = –1, min = 1000;
        int ans;
        scanf("%s", str);
        len = strlen(str);
        for(i = 0; i < len; i++){
                count[str[i] – ‘a’]++;
        }
        for(i = 0; i < 26; i++){
                if(max < count[i]){
                        max = count[i];
                }
                if(min > count[i] && count[i] != 0){
                                //如果字符没有出现的话就不算 
                        min = count[i];
                }
        }
        ans = max – min;
        if(isprime(ans)){
                printf("Lucky Word\n");
                printf("%d\n", ans);
        }else{
                printf("No Answer\n0\n");
                        //忘记输出0了  
        }
//      getch();
        return 0;
}

发表回复

您的电子邮箱地址不会被公开。