CODE[VS] 1462 素数和

题目描述 说明

给定2个整数a,b求出它们之间(不含a,b)所有质数的和。

输入描述 输入描述

一行,AB(0 <= A,B <= 65536)

输出描述 输出描述

一行,A,B之间(不含A,B)所有素数的和。

样例输入 示例输入

39 1224

样例输出 示例输出

111390

数据范围及提示 数据大小和提示

注意没有要求A <B

看到了热搜:

地铁battle站。。

没忍住,点开了。

有这就是街舞的内容!!!

哎,到了高中真的是啥也看不了哇。

看到了亮亮和杨文昊的battle。

太燃了!!!

里面有首歌,什么我是个机器人摇摇摆摆。。

愣是没搜到。

燃。

有空再看你们!

加油,街舞正是时代潮流!

看起来很简单,于是自信的打出了第一份代码。

就是从a到b枚举,判断是不是素数,是就加。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7
 8 int a,b;
 9 long long ans;
10
11 bool judge(int x)
12 {
13     for(int i=2;i<=sqrt(x);++i)
14         if(x%i==0) return false;
15     return true;
16 }
17
18 int main()
19 {
20     scanf("%d%d",&a,&b);
21     for(int i=a;i<=b;++i)
22     {
23         if(judge(i))
24             ans+=i;
25     }
26     printf("%lld",ans);
27     return 0;
28 }

40分,woc哪儿有问题。。

看了一个没过的样例,,,

b居然比a小。

made,太坑了。

于是马上改了第二份代码,提交。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

int a,b;
long long ans;

bool judge(int x)
{
    for(int i=2;i<=sqrt(x);++i)
        if(x%i==0) return false;
    return true;
}

int main()
{
    scanf("%d%d",&a,&b);
    if(a>b) swap(a,b);
    for(int i=a;i<=b;++i)
    {
        if(judge(i))
            ans+=i;
    }
    printf("%lld",ans);
    return 0;
}

woc90分,

哪儿又错了。

没过的样例里有1,

那就会从1开始枚举,把1给加上啊!

千辛万苦,终于打出了ac代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7
 8 int a,b;
 9 long long ans;
10
11 bool judge(int x)
12 {
13     for(int i=2;i<=sqrt(x);++i)
14         if(x%i==0) return false;
15     return true;
16 }
17
18 int main()
19 {
20     scanf("%d%d",&a,&b);
21     if(a>b) swap(a,b);
22     for(int i=a;i<=b;++i)
23     {
24         if(i==1) continue;
25         if(judge(i))
26             ans+=i;
27     }
28     printf("%lld",ans);
29     return 0;
30 }

yes!!!


如果你不开心,那我就把右边这个zz大炸分享给你吧,你看,他这么好看,跟个大傻子一样看着你,你还伤心吗?真的!这照片盯上他五秒钟就想笑了。一切都会过去的。时间时间会给你答案2333

原文地址:https://www.cnblogs.com/Mary-Sue/p/9169072.html

时间: 2024-08-27 13:26:27

CODE[VS] 1462 素数和的相关文章

codevs——1462 素数和

1462 素数和 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 青铜 Bronze 题解 题目描述 Description 给定2个整数a,b 求出它们之间(不含a,b)所有质数的和. 输入描述 Input Description 一行,a b(0<=a,b<=65536) 输出描述 Output Description 一行,a,b之间(不含a,b)所有素数的和. 样例输入 Sample Input 39 1224 样例输出 Sample Output 111390 数据范

1462 素数和 codevs

题目描述 Description 给定2个整数a,b 求出它们之间(不含a,b)所有质数的和. 输入描述 Input Description 一行,a b(0<=a,b<=65536) 输出描述 Output Description 一行,a,b之间(不含a,b)所有素数的和. 样例输入 Sample Input 39 1224 样例输出 Sample Output 111390 数据范围及提示 Data Size & Hint 注意没有要求a<b 1 #include <

codevs:1462 素数和:给定2个整数a,b 求出它们之间(不含a,b)所有质数的和。

#include<iostream>#include<cstdio>#include<cmath>using namespace std;int main(){ int flag=0,a,b,tot=0; scanf("%d%d",&a,&b); if(a>b)swap(a,b); for(int i=a+1;i<b;++i) { flag=1; for(int j=2;j<=sqrt(i);++j) { if(i%

【数论】【筛法求素数】CODEVS 1462 素数和

好吧……我不会欧拉筛也就罢了…… 傻逼筛法竟然这么长时间以来 一直RE ……源头竟然是 int 爆了. 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 bool vis[70000]; 5 int a,b; long long ans; 6 void Shai() 7 { 8 vis[1]=true; 9 for(long long i=2;i<=65536;i++) 10 for(long

CodeForces114E——Double Happiness(素数二次筛选)

Double Happiness On the math lesson a teacher asked each pupil to come up with his own lucky numbers. As a fan of number theory Peter chose prime numbers. Bob was more original. He said that number t is his lucky number, if it can be represented as:t

HDOJ4548-美素数(前缀和,线性筛)

Problem Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数. 给定一个区间,你能计算出这个区间内有多少个美素数吗? Input 第一行输入一个正整数T,表示总共有T组数据(T <= 10000).接下来共T行,每行输入两个整数L,R(1<= L <= R <

Sky Code(poj3904)

Sky Code Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2085   Accepted: 665 Description Stancu likes space travels but he is a poor software developer and will never be able to buy his own spacecraft. That is why he is preparing to ste

BZOJ-1053-反素数ant

描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0 < i < x,则称x为反质数.例如,整数1,2,4,6等都是反质数. 现在给定一个数N,你能求出不超过N的最大的反质数么? 分析 一个数约数个数=所有素因子的次数+1的乘积 一个2000000000以内的数字不会有超过12个素因子 较小的数的指数一定大于等于较大的数的指数 准备工作: 预处理出前12个素数. 然后就可以暴搜了, 将递归层数设定为第 dep 个

poj 3904 Sky Code

点击打开链接 Sky Code Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1562   Accepted: 478 Description Stancu likes space travels but he is a poor software developer and will never be able to buy his own spacecraft. That is why he is preparing