UVa 10924 Prime Words (素数)

262 篇文章 0 订阅
100 篇文章 0 订阅

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值