mirror of
https://github.com/renbaoshuo/202401-programming-assignments.git
synced 2024-11-27 08:26:20 +00:00
49 lines
1.0 KiB
Markdown
49 lines
1.0 KiB
Markdown
|
# 6-6 使用函数求素数和
|
|||
|
|
|||
|
本题要求实现一个判断素数的简单函数、以及利用该函数计算给定区间内素数和的函数。
|
|||
|
|
|||
|
素数就是只能被1和自身整除的正整数。注意:1不是素数,2是素数。
|
|||
|
|
|||
|
### 函数接口定义:
|
|||
|
```c++
|
|||
|
int prime( int p );
|
|||
|
int PrimeSum( int m, int n );
|
|||
|
```
|
|||
|
其中函数`prime`当用户传入参数`p`为素数时返回1,否则返回0;函数`PrimeSum`返回区间[`m`, `n`]内所有素数的和。题目保证用户传入的参数`m`$$\le$$`n`。
|
|||
|
|
|||
|
### 裁判测试程序样例:
|
|||
|
```c++
|
|||
|
#include <stdio.h>
|
|||
|
#include <math.h>
|
|||
|
|
|||
|
int prime( int p );
|
|||
|
int PrimeSum( int m, int n );
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
int m, n, p;
|
|||
|
|
|||
|
scanf("%d %d", &m, &n);
|
|||
|
printf("Sum of ( ");
|
|||
|
for( p=m; p<=n; p++ ) {
|
|||
|
if( prime(p) != 0 )
|
|||
|
printf("%d ", p);
|
|||
|
}
|
|||
|
printf(") = %d\n", PrimeSum(m, n));
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
/* 你的代码将被嵌在这里 */
|
|||
|
```
|
|||
|
|
|||
|
### 输入样例:
|
|||
|
```in
|
|||
|
-1 10
|
|||
|
```
|
|||
|
|
|||
|
### 输出样例:
|
|||
|
```out
|
|||
|
Sum of ( 2 3 5 7 ) = 17
|
|||
|
```
|