4 UVA 10924 Prime Words

筛素数,把给的单词中出现的字母对应值加起来,判断是否是质数。

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=2000;
int pri[maxn+10];
char s[100];
void init()
{
    int i,j;
    memset(pri,1,sizeof pri);
    pri[0]=0;
    for(i=2;i<=(maxn>>1);i++)
    {
        if(!pri[i]) continue;
        for(j=i+i;j<=maxn;j+=i)
            pri[j]=0;
    }
}

int main()
{
    int i,len;
    init();
    while(~scanf("%s",s))
    {
        len=strlen(s);
        int ans=0;
        for(i=0;i<len;i++)
        {
            if(s[i]>='a'&&s[i]<='z') ans+=s[i]-'a'+1;
            else if(s[i]>='A'&&s[i]<='Z') ans+=s[i]-'A'+27;
        }
        if(pri[ans]) puts("It is a prime word.");
        else puts("It is not a prime word.");
    }
    return 0;
}
时间: 2024-08-02 19:10:46

4 UVA 10924 Prime Words的相关文章

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

UVA 10140 - Prime Distance(数论)

10140 - Prime Distance 题目链接 题意:求[l,r]区间内最近和最远的素数对. 思路:素数打表,打到sqrt(Max)即可,然后利用大的表去筛素数,由于[l, r]最多100W,所以可以去遍历一遍,找出答案.注意1的情况,一开始没判断1,结果WA了 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define INF 0x3f

UVa 524 - Prime Ring Problem

题目:把1-n,连续的放到一个环里,使相邻的数字和为素数,输出所有结果. 分析:搜索+剪枝.如果裸搜,用dancing-links那种拆装的链表,应该差不多满足16的数据量. 这里利用一个性质进行剪枝:相邻的数字一定是奇偶性不同的数字. (如果上述假设不成立,则存在相邻的奇数或偶数,那么他们的和一定是大于2的偶数,不是素数) 根据上面的假设,还有一条推论:只有n为偶数时才有解. (n为奇数,一直至少有一对奇数相邻,同上,矛盾(鸽巢原理)) 因此,每次搜索的数据其实是n/2,时间复杂度为O((n/

UVA - 524 Prime Ring Problem(dfs回溯法)

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number

uva 1404 - Prime k-tuple(数论)

题目链接:uva 1404 1404 - Prime k-tuple 题目大意:如果k个相邻的素数p1,p2,-,pk,满足pk?p1=s,称这些素数组成一个距离为s的素数k元组,给定区间a,b,求有多少个距离s的k元组. 解题思路:筛选素数法,先预处理出[1, sqrt(inf)]的素数表,然后对给定区间[a,b]根据预处理出的素数表筛选出素数即可. #include <cstdio> #include <cstring> #include <cmath> #incl

UVA 1404 - Prime k-tuple(素树筛选)

UVA 1404 - Prime k-tuple 题目链接 题意:找出a-b之间有多少个素数k元组,并且最后一个元素减第一个元素为s 思路:先筛出sqrt的素数,然后对于每个区间,在用这些素数去筛出区间的素数,然后twopointer搞一下即可 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <vector> usi

UVA 10200 Prime Time 水

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1141 题意:判断区间[a,b]的f(i)是否为素数,f(i)=i*i+i+40: 思路:打个表,然后注意精度: #include<bits/stdc++.h> using namespace std; #define ll long long #define esp

UVA 10200 Prime Time 暴力水题

一个简单的暴力水题,只是输出方式让人无语... #include <stdio.h> #include <string.h> int prime(int n) { int i; for(i=2;i*i<=n;i++) { if((n%i)==0) return 0; } return 1; } int main() { int num[10010]; int i; int a,b; int sum; memset(num,0,sizeof(num)); for(i=0;i&l

uva 524 prime ring problem——yhx

  Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural numbers into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always