[南阳OJ-No.22]素数求和问题|现在给你N个数(0<N<1000),现在要求你写出一个程序,找出这N个数中的所有素数,并求和。

本文介绍了一道编程题目,要求实现一个程序找出给定数组中的所有素数并计算其总和。提供了多种编程语言的实现示例,包括Java和C++。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

南阳OJ-No.22

时间限制3000ms,内存限制65535KB

描述

现在给你N个数(0 < N < 1000),现在要求你写出一个程序,找出这N个数中的所有素数,并求和。

输入

第一行给出整数M(0 < M < 10)代表多少组测试数据
每组测试数据第一行给你N,代表该组测试数据的数量。
接下来的N个数为要测试的数据,每个数小于1000

输出

每组测试数据结果占一行,输出给出的测试数据的所有素数和

样例输入

3
5
1 2 3 4 5
8
11 12 13 14 15 16 17 18
10
21 22 23 24 25 26 27 28 29 30

样例输出

10
41
52


java

时间135,内存1515

import java.util.Scanner;

public class Main {
    public static Scanner cin = new Scanner(System.in);

    public static void main(String[] args) throws Exception{
            int line = cin.nextInt();
            int sum;
            int[] s;

            while(line>=1) {
                s = getInt();
                sum = 0;
                for (int i=0; i<s.length; i++) {
                    if(isPrime(s[i])) {
                        sum += s[i];
                    }
                }
                System.out.println(sum);
                line --;
            }
    }

    public static int[] getInt() {
        int x = cin.nextInt();
        int[] s = new int[x];

        for(int i=0; i<s.length; i++) {
            s[i] = cin.nextInt();
        }

        return s;
    }

    public static boolean isPrime(int x) {
        if (x == 1) {
            return false;
        }

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

时间140,内存1523

import java.util.Scanner;

public class Main {
    public static Scanner cin = new Scanner(System.in);
    public static void main(String[] args) throws Exception{
            int line=cin.nextInt();
            int sum;
            int[] s;
            while(line>=1){
                s = getInt();
                sum = 0;
                for (int i=0; i<s.length; i++)
                    if(isPrime(s[i]))
                        sum += s[i];
                System.out.println(sum);
                line --;
            }
    }

    public static int[] getInt(){
        int x = cin.nextInt();
        int[] s = new int[x];
        for(int i=0; i<s.length; i++)
            s[i] = cin.nextInt();
        return s;
    }

    public static boolean isPrime(int x){
        if (x == 1)
            return false;
        for (int i=2; i<x; i++)
            if(x%i == 0)
                return false;
        return true;
    }
}

仅仅把括号省略,以为更简洁,反而系统不情愿???注意书写规范

时间50,内存61
仅供参考

import java.io.IOException;

public class Main {
    public static int getInt() throws IOException{
        int i;
        while((i = System.in.read()) < 48 || i > 57);
        int temp = 0;
        while(i > 47 && i < 58){
            temp = temp * 10 + i - 48;
            i = System.in.read();
        }
        return temp;
    }

    public static boolean isPrime(int n){
        if(n < 2)
            return false;
        int len = (int)Math.sqrt(n) + 1;
        for(int i = 2;i < len; i++)
            if(n%i == 0)
                return false;
        return true;
    }

    public static void main(String args[]) throws IOException{
        int i, j, t, sum;
        for (i = getInt(); i > 0; i--) {
            sum = 0;
            for(j = getInt(); j > 0; j--)
                if (isPrime(t = getInt()))
                    sum += t;
            System.out.println(sum);
        }
    }
} 

C++

时间12,内存240

#include<iostream> 
using namespace std;

bool isPrime(int x)
{
  if(x==1)
    return false;

  for(int i=2; i<x; i++)
    if(x%i==0)
      return false;

  return true;
}

int main()
{
  int line, base, sum;

  cin >> line;

  while(line--)
  {
    sum = 0;
    cin >> base;
    int s[base];
    for(int i=0; i<base; i++)
      cin >> s[i];

    for(int i=0; i<base; i++)
      if(isPrime(s[i]))
        sum += s[i];

    cout << sum << endl;
  }
  return 0;
}

时间4,内存240

#include<stdio.h>
int prime(int n)
{
    int a=2;
    while(n>=a)
        if(!(n%a++))
            break;
    if(a==n+1&&n!=1)
        return 1;
    return 0;
}
int main()
{
    int n,m;
    int a[1024];
    scanf("%d",&n);
    while(n--)
    {
        int sum=0;
        scanf("%d",&m);
        for(int i = 0;i<m;++i)
            scanf("%d",&a[i]);
        for(int i = 0;i<m;++i)
            if(prime(a[i]))
            sum+=a[i];
        printf("%d\n",sum);
    }
   return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值