1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-12-16 15:44:39 +00:00
202401-programming-assignments/【实践课内】11.函数2/6-1 使用函数输出指定范围内的完数.md

57 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 6-1 使用函数输出指定范围内的完数
本题要求实现一个计算整数因子和的简单函数,并利用其实现另一个函数,输出两正整数$$m$$和$$n$$$$0<m\le n\le 10000$$之间的所有完数所谓完数就是该数恰好等于除自身外的因子之和例如6=1+2+3其中1、2、3为6的因子。
### 函数接口定义:
```c++
int factorsum( int number );
void PrintPN( int m, int n );
```
其中函数`factorsum`须返回`int number`的因子和;函数`PrintPN`要逐行输出给定范围[`m`, `n`]内每个完数的因子累加形式的分解式,每个完数占一行,格式为“完数 = 因子1 + 因子2 + ... + 因子k”其中完数和因子均按递增顺序给出。如果给定区间内没有完数则输出一行“No perfect number”。
### 裁判测试程序样例:
```c++
#include <stdio.h>
int factorsum( int number );
void PrintPN( int m, int n );
int main()
{
int m, n;
scanf("%d %d", &m, &n);
if ( factorsum(m) == m ) printf("%d is a perfect number\n", m);
if ( factorsum(n) == n ) printf("%d is a perfect number\n", n);
PrintPN(m, n);
return 0;
}
/* 你的代码将被嵌在这里 */
```
### 输入样例1
```in
6 30
```
### 输出样例1
```out
6 is a perfect number
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
```
### 输入样例2
```in
7 25
```
### 输出样例2
```out
No perfect number
```
**鸣谢杭州电子科技大学网络空间安全学院李丰同学修正数据!**