素数的求和(acm)

题目要求是先输入一个数据来确定有几组数据,再输入一个数据来确定这一组数中有几个数据,然后在输入每组数的数据,最终显示出每组数中素数之和。代码如下:

#include <stdio.h>

int Judge(int a);

int main()

{  int num;

int a[10][1000];

int i,j;

int sum = 0;

scanf("%d",&num);

for(i = 0;i < num;i++)

{

scanf("%d",&a[i][0]);

for(j = 1;j <= a[i][0];j++)

{

scanf("%d",&a[i][j]);

}

}

for(i = 0;i < num;i++)

{

for(j = 1;j <= a[i][0];j++)

{

if(Judge(a[i][j]) == 1)

sum += a[i][j];

}

printf("%d\n",sum);

sum = 0;

}

return 0;

}

int Judge(int a)

{  int i;

if(a == 1)

return 0;

for(i = 2;i < a;i++)

{

if(a%i == 0)

break;

}

if(i>=a)

return 1;

else

return 0;

}

时间: 2024-08-09 02:20:33

素数的求和(acm)的相关文章

循环-15. 统计素数并求和

循环-15. 统计素数并求和(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 张彤彧(浙江大学) 本题要求统计给定整数M和N区间内素数的个数并对它们求和. 输入格式: 输入在一行中给出2个正整数M和N(1<=M<=N<=500). 输出格式: 在一行中顺序输出M和N区间内素数的个数以及它们的和,数字间以空格分隔. 输入样例: 10 31 输出样例: 7 143 1 #include<stdio.h> 2

循环-15. 统计素数并求和(20)

1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 bool isPrime(int n){ 5 int i; 6 for(i=2;i<=sqrt(n);++i) 7 if(n%i==0) 8 break; 9 if(n>=2&&i>sqrt(n)) 10 return true; 11 else 12 return false; 13 } 14 int main(){ 1

04-3. 统计素数并求和(20)

本题要求统计给定整数M和N区间内素数的个数并对它们求和. 输入格式: 输入在一行中给出2个正整数M和N(1<=M<=N<=500). 输出格式: 在一行中顺序输出M和N区间内素数的个数以及它们的和,数字间以空格分隔. 输入样例: 10 31 输出样例: 7 143 注:此题先统计素数,每统计到一个,计数器加1,并进行求和操作即可 #include "stdio.h" int main() { int M,N; scanf("%d %d",&

POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Accepted: 10619 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations d

POJ 2739 Sum of Consecutive Prime Numbers(素数)

http://poj.org/problem?id=2739 题意: 给你一个10000以内的自然数X,然后问你这个数x有多少种方式能由连续的素数相加得来? 分析: 首先用素数筛选法把10000以内的素数都找出来按从小到大保存到prime数组中. 然后找到数X在prime中的上界, 如果存在连续的素数之和==X, 那么一定是从一个比X小的素数开始求和(不会超过X的上界),直到和sum的值>=X为止. 所以我们暴力枚举10000以内的所有可能的素数相加和的起始点i,然后求连续素数的和,看看当前以p

wenbao与网络赛(百亿内素数)

@ 求1到一百亿之内的素数个数 http://acm.hdu.edu.cn/showproblem.php?pid=5901 @  复杂度大概O(n^(3/4)) 1 //G++ 1560ms 6544k 2 #include <bits/stdc++.h> 3 #define ll long long 4 using namespace std; 5 ll f[340000],g[340000],n; 6 void init(){ 7 ll i,j,m; 8 for(m=1;m*m<=

projecteuler Summation of primes

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. 译文: 10以下的素数之和为17,求出2000000以下的素数之和. ======================= 第一次code: 1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(

[家里蹲大学数学杂志]第242期浙江大学2013年数学分析考研试题参考解答

1($4\times 10'=40'$) (1)$\dps{\lim_{x\to 0}\frac{\sin x-\arctan x}{\tan x-\arcsin x}}$. 解答: $$\beex \bea \lim_{x\to 0}\frac{\sin x-\arctan x}{\tan x-\arcsin x} &=\lim_{x\to 0}\frac{\sez{x-\frac{x^3}{6}+o(x^3)}-\sez{x-\frac{x^3}{3}+o(x^3)}}{ \sez{x+\f

[LightOJ1370]Bi-shoe and Phi-shoe

A - Bi-shoe and Phi-shoe Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular c