UVa 10924 Prime Words:素數

來源:互聯網
上載者:User

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

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.