24h購物| | PChome| 登入
2012-04-04 06:48:36| 人氣1,377| 回應0 | 上一篇 | 下一篇

[UVA] 10924 - Prime Words

推薦 0 收藏 0 轉貼0 訂閱站台

Problem E - Prime Words

Time Limit: 1 second

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 specific 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 file (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.

Problem setter: Sérgio Queiroz de Medeiros

#include <stdio.h>
int Prime[2000] = {};
void sieve() {
    int i, j;
    for(i = 2; i < 2000; i++) {
        if(Prime[i] == 0) {
            for(j = 2; i*j < 2000; j++)
                Prime[i*j] = 1;
        }
    }
}
int main() {
    sieve();
    char str[100];
    while(scanf("%s", str) == 1) {
        int sum = 0, i;
        for(i = 0; str[i]; i++) {
            if(str[i] >= 'a' && str[i] <= 'z')
                sum += str[i]-'a'+1;
            else
                sum += str[i]-'A'+27;
        }
        if(Prime[sum] == 0)
            puts("It is a prime word.");
        else
            puts("It is not a prime word.");
    }
    return 0;
}

台長: Morris
人氣(1,377) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10921 - Find the Telephone
此分類上一篇:[UVA][Greedy] 10720 - Graph Construction

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文