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 this has only two divisors:itself and the number one. Examples of prime numbers are:1, 2, 3, 5,, and 10007.
In this problem your 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 are worth 1, letter B are worth2 and so on until letter Z-is worth 26. In the same way, letter A is worth, letterb are worth and letter Z are worth52.
You are should write a program to determine if a word was a prime word or not. A word is a prime word if the sum of the IT letters is a prime number.
Input
The input consists of a set of words. Each word was 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 are a prime word., if the sum of the letters of the word is a prime number, otherwise yo U 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.
Water problem.
Complete code:
/*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 &: (*p &) +);
Notprime = false;
for (i = 2; I * i <= sum; ++i)///because the largest also 52*20=1040, the open root of the whole is 32, so directly calculate
if (sum% i = = 0)
{
notprime = true;< C18/>break;
}
Puts (notprime?) ' It is not a prime word. ': ' It is a prime word. '
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/