Exercise 6-3 Use a function to output the complete number within a specified range (20 point(s))

This question requires the realization of a simple function to calculate the sum of integer factors, and use it to realize another function, outputting all the complete numbers between two positive integers m and n (0<m≤n≤10000). The so-called end number is that the number is exactly equal to the sum of factors other than itself. For example: 6=1+2+3, where 1, 2, 3 are factors of 6.

Function interface definition:

int factorsum( int number );
void PrintPN( int m, int n );

The function factorsum must return the sum of the factors of the int number; the function PrintPN must output the factor accumulation form of the factor accumulation of the given range [m, n] line by line, and each finished number occupies one line, and the format is "finished number" = Factor 1 + factor 2 +… + factor k", where the final number and factor are given in increasing order. If there is no perfect number in the given interval, output a line of "No perfect number".

Sample referee test procedure:

#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;
}

/* 你的代码将被嵌在这里 */

Input example 1:

6 30

Output sample 1:

6 is a perfect number
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14

Input example 2:

7 25

Output sample 2:

No perfect number

answer:

int factorsum( int number )
{
    
     //完数就是该数恰好等于除自身外的因子之和
	int sum = 1; //定义sum为该数因子之和(由于1是每个数都有的因子,所以直接sum等一1)
	int i; //循环变量 同时也是因子
	for (i = 2; i < number; i++) //循环从2到number减一(每个数的因子都有1)
	{
    
    
		if (number % i == 0) sum += i; //如果能被2整除就是因子,累加y起来
	}
	return sum;
}
void PrintPN( int m, int n )
{
    
    
	int flag = 0; //中介(如果有完数赋值为1,如果没有就是0)
	int i, s; //循环变量 (同时i表示区间的每个数,s表示因子)
	for (i = m;i <= n; i++) //区间循环
	{
    
    
		if (factorsum(i) == i) //如果调用上面的函数的求因子的和等于我们现在的数i,那么i就是完数
		{
    
     //如果 一个数的因子之和 等于这个数 那么该数就是完数 
			flag = 1; //有完数 flag赋值为1
			printf("%d = 1", i); //由于每一个数都有因子1,所系先打印出来
			for (s = 2; s < i; s++) //找因子
			{
    
    
				if (i % s == 0) printf(" + %d",s); //打印因子(格式需要找规律)
			}
			printf("\n"); //打印完一个完数需换行
		}
	}
	if (flag == 0) printf("No perfect number"); //如果没有完数,打印相应信息
}

------------------------------Another way of writing---------------- ----------------------------------------

// int factorsum( int number )
// { //完数就是该数恰好等于除自身外的因子之和
// 	int sum = 1; //定义sum为该数因子之和(由于1是每个数都有的因子,所以直接sum等一1)
// 	int i; //循环变量 同时也是因子
// 	for (i = 2; i < number; i++) //循环从2到number减一(每个数的因子都有1)
// 	{
    
    
// 		if (number % i == 0) sum += i; //如果能被2整除就是因子,累加y起来
// 	}
// 	return sum;
// }
// void PrintPN( int m, int n )
// {
    
    
// 	int i, s; //循环变量(同时i表示区间的每个数,s表示因子) 
// 	int cnt = 0; //中介,同时也表计数器,如果大于0就表示有完数 
// 	for (i = m; i <= n; i++) //区间循环
// 	{
    
    
// 		int sum = 1; //一个数的因子之和 
// 		for (s = 2; s < i; s++) //循环求一个数的因子之和 
// 		{
    
    
// 			if (i % s == 0) sum+=s;
// 		}
// 		if (sum == i) //如果 一个数的因子之和 等于这个数 那么该数就是完数 
// 		{
    
    
// 			cnt++; //计数器加一 
// 			printf("%d = 1", i); //由于每一个数都有因子1,所系先打印出来
// 			for (s = 2; s < i; s++) //找因子
// 			{ //6 = 1 + 2 + 3
// 				if (i % s == 0) printf(" + %d", s); //打印因子(格式需要找规律)
// 			}
// 			printf("\n"); //打印完一个完数需换行
// 		}
// 	}
// 	if (cnt == 0) printf("No perfect number"); //如果没有完数,打印相应信息
// } 

Guess you like

Origin blog.csdn.net/qq_44715943/article/details/114584769