有两个整数,如果每个整数的约数和(除了它本身以外)等于对方,我们就称这对数是友好的。

/*
有两个整数,如果每个整数的约数和(除了它本身以外)等于对方,我们就称这对数是友好的。例如:
9的约数和有:1+3=4
4的约数和有:1+2=3
所以9和4不是友好的。
220的约数和有:1  2  4  5  10  11  20  22  44  55  110=284
284的约数和有:1  2  4  71  142=220
所以220和284是友好的。
编写程序,判断两个数是否是友好数。
*/
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#include <malloc.h>
int func(int n){
    int i,sum;
    sum=0;
    for(i=1;i<=n/2;i++){
        if(n%i==0){
            sum+=i;
        }
    }
    return sum;
}
int main()
{
    int n,m,rs1,rs2;
    scanf("%d%d",&n,&m);
    rs1=func(n);
    rs2=func(m);
    if(rs1==m&&rs2==n){
        printf("%d is friendship with %d
",n,m);
    }else{
        printf("%d is not friendship with %d
",n,m);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zhaohuan1996/p/11840289.html