码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-02 12:31:13      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   strong   io   for   2014   

 1 /*
 2  * Main.c
 3  * C15-循环-15. 统计素数并求和
 4  *  Created on: 2014年8月1日
 5  *      Author: Boomkeeper
 6  ***********测试通过*****
 7  */
 8 
 9 #include <stdio.h>
10 
11 int sum = 0, count = 0; //记录素数和以及素数个数
12 int *p_sum = &sum, *p_count = &count;
13 /*
14  * 找出素数并求和计数
15  */
16 void findPrime(int M, int N) {
17 
18     int i, j, flag = 0;
19 
20     for (i = M; i <= N; i++) {
21         if ((i > 2) && (i % 2 == 0))
22             continue;
23         for (j = 1; j <= i; j++) {
24             if (i % j == 0) {
25                 flag++;
26             }
27         }
28         if (flag == 2) {
29             *p_sum += i;
30             (*p_count)++;
31         }
32         flag = 0;
33     }
34 }
35 
36 int main(void) {
37 
38     int M, N;
39 
40     scanf("%d %d", &M, &N);
41     findPrime(M, N);
42     printf("%d %d\n", count, sum);
43 
44     return 0;
45 
46 }

题目链接:

http://pat.zju.edu.cn/contests/basic-programming/%E5%BE%AA%E7%8E%AF-15

 

    

循环-15. 统计素数并求和,布布扣,bubuko.com

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

标签:style   blog   http   color   strong   io   for   2014   

原文地址:http://www.cnblogs.com/boomkeeper/p/C15.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!