Welcome

首页 / 软件开发 / 数据结构与算法 / UVa 10924 Prime Words:素数

UVa 10924 Prime Words:素数2014-07-30 synapse7 10924 - Prime Words

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1865

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 rangea-z and A-Z. Each letter has a specific value, the lettera is worth 1, letter b is worth2 and so on until letter z that is worth 26. In the same way, letter A is worth 27, letterB is worth 28 and letter Z is worth52.

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 hasL 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.

水题。

完整代码:

/*0.009s*/#include<cstdio>char s[25];int main(){int i, sum;char* p;bool notprime;while (gets(s)){sum = 0;for (p = s; *p; ++p)sum += (*p >= "a" ? *p & 31 : (*p & 31) + 26);notprime = false;for (i = 2; i * i <= sum; ++i)///由于最大也就52*20=1040,开根号下取整就是32,所以直接算if (sum % i == 0){notprime = true;break;}puts(notprime ? "It is not a prime word." : "It is a prime word.");}return 0;}