UVA 10924 Prime Words 题解

Prime Words

A prime number is a number that has only two divisors: itself and the number one. Examples of prime
numbers are: 1, 2, 3, 5, 17, 101 and 10007.
In this problem you should read a set of words, each word is composed only by letters in the range
a-z and A-Z. Each letter has a speci c value, the letter a is worth 1, letter b is worth 2 and so on until
letter z that is worth 26. In the same way, letter A is worth 27, letter B is worth 28 and letter Z is worth
52.
You should write a program to determine if a word is a prime word or not. A word is a prime word
if the sum of its letters is a prime number.
Input
The input consists of a set of words. Each word is in a line by itself and has L letters, where 1  L  20.
The input is terminated by enf of le (EOF).
Output
For each word you should print: `It is a prime word.', if the sum of the letters of the word is a
prime number, otherwise you should print: `It is not a prime word.'.
Sample Input
UFRN
contest
AcM
Sample Output
It is a prime word.
It is not a prime word.
It is not a prime word.

题意:给定一个串,在a~z对应1~26,A~Z对应1+26~26+26的情况下计算这个串的数值,如果这个数值是素数,输出“It is a prime word.”否则输出“It is not a prime word.”。

分析:水题,直接上代码。

#include<bits/stdc++.h>
using namespace std;
map<char,int> book;
bool u[100005];
void ass()
{
    memset(u,true,sizeof(u));
    for(int i=2;i<=100005;i++)
    {
        if(u[i])
        {
            for(int j=2;j<=100005;j++)
            {
                if(j*i>100005)    break;
                u[j*i]=false;
            }
        }
    }
}
void pre()
{
    for(char ch='a';ch<='z';ch++)
    {
        book[ch]=ch-'a'+1;
    }
    for(char ch='A';ch<='Z';ch++)
    {
        book[ch]=ch-'A'+1+26;
    }
}
int main()
{
    //freopen("input.txt","r",stdin);
    pre();
    ass();
    char s[25];
    while(~scanf("%s",s))
    {
        int len=strlen(s);
        int tmp=0;
        for(int i=0;i<len;i++)
        {
            tmp+=book[s[i]];
        }
        if(u[tmp])    printf("It is a prime word.\n");
        else printf("It is not a prime word.\n");
    }
    return 0;
}
View Code

 

posted on 2019-08-29 10:01  Caution_X  阅读(276)  评论(0编辑  收藏  举报

导航