• 31: 素数和


    31 素数和

    作者: XXX 时间限制: 1S 章节: 循环

    问题描述 :
    求1~n之间所有素数的和

    输入说明 :
    第一行,输入N
    以下N行,每行一个数字n(N<100,n<1000)

    输出说明 :
    N行,每行为1~n(包括n)之间素数的和

    输入范例 :
    3
    3
    10
    11
    输出范例 :
    5
    17
    28

    代码:

    #include <stdio.h>
    int main()
    {
    	int N, n;
    	scanf("%d", &N);
    	int hash[1000] = { 0 };
    	hash[0] = hash[1] = 1;
    	int sum = 0;
    	for (int i = 2; i < 1000; i++)
    	{
    		if (hash[i] == 0)
    		{
    			for (int j = i+i; j < 1000; j=j+i)
    			{
    				hash[j] = 1;
    			}
    		}
    	}
    	for (int i = 0; i < N; i++)
    	{
    		scanf("%d", &n);
    		for (int j = 2; j <= n; j++)
    		{
    			if (hash[j] == 0)
    			{
    				sum += j;
    			}
    		}
    		printf("%d
    ", sum);
    		sum = 0;
    	}
    	return 0;
    }
    
    Yesterday is history,tomorrow ismystery,but today is a gift!That why it is called Present!
  • 相关阅读:
    Codeforces 376A. Night at the Museum
    Assigning Workstations
    树的直径证明
    Frogger
    Circle
    HDU 1022 Train Problem I
    Argus
    树状数组总结
    C++ 容器(一):顺序容器简介
    C++ 输出缓冲区的管理
  • 原文地址:https://www.cnblogs.com/VictorierJwr/p/12408697.html
Copyright © 2020-2023  润新知