实例四十二:因子和问题

实例四十二:因子和问题

问题描述:
输出 1-N (N<32 768) 之间所有同时满足下列条件的整数对(A,B):
(1)3<A<B<=N;
(2)A 的因子之和等于 B,且 B 的因子之和等于 A ( X 的因子和是指除了 X 以外的所有因子之和)。
Outputs an integer pair (A, B) between 1-N (N<32 768) that satisfies the following conditions:
(1)3<A<B<=N;
(2) The sum of the factors of A is equal to B, and the sum of the factors of B is equal to A (the factor sum of X refers to the sum of all factors except X).

算法思路:

先求出 4~N 之间的任意数 A 的因子和 X,若 A<X<=N,且 X 的因子和等于 A ,则(A,X)即为满足条件的解;否则,就不存在与 A 配对的 B。
First find the factor of any number A between 4 and N and X. If A<X<=N, and the factor sum of X is equal to A, then (A, X) is the solution that satisfies the condition; otherwise, there is no pairing with A. B.

/*实例四十二:因子和问题*/
#include <stdio.h>

int sumy(int n)
{
    int i,j,s;
    s=1;
    i=2;
    do
    {
        j=n/i;
        if(n%i==0)
            if(i==j)
                s+=i;
        else                //可能产生歧义2019.3.31
                s+=i+j;
        i++;
    }while(i<j);
    return s;
}

int main(void)
{
    int i,x,n;
    printf("Please enter the integer value of n:\n");
    scanf("%d",&n);
    for(i=4;i<n;i++)
    {
        x=sumy(i);
        if(x>i&&x<=n)
            if(i==sumy(x))
                printf("%d%d\n",i,x);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值