发布网友 发布时间:2023-03-22 10:27
共1个回答
热心网友 时间:2023-10-10 19:02
用函数求相亲数的程序及运行实例:
#include<stdio.h>
int sum1(int n)
{int i,s=1;
for(i=2;i<=n/2;i++)
if(n%i==0)s+=i;
return s;
}
int main()
{ int a,b,i,j,t;
scanf("%d%d",&a,&b);
for(i=a; i<=b; i++)
{t=sum1(i);
if(t>i&&sum1(t)==i)
{printf("%d:1",i);
for(j=2;j<=i/2;j++)
if(i%j==0)printf("+%d",j);
printf("=%d\n",t);
printf("%d:1",t);
for(j=2;j<=t/2;j++)
if(t%j==0)printf("+%d",j);
printf("=%d\n",i);
}
}
return 0;
}
=================
#include<iostream>
using namespace std;
int sum1(int n)
{int i,s=1;
for(i=2;i<=n/2;i++)
if(n%i==0)s+=i;
return s;
}
int main()
{ int a,b,i,j,t;
cin>>a>>b;
for(i=a; i<=b; i++)
{t=sum1(i);
if(t>i&&sum1(t)==i)
{cout<<i<<":1";
for(j=2;j<=i/2;j++)
if(i%j==0)cout<<"+"<<j;
cout<<"="<<t<<endl;
cout<<t<<":1";
for(j=2;j<=t/2;j++)
if(t%j==0)cout<<"+"<<j;
cout<<"="<<i<<endl;
}
}
return 0;
}