C语言高手帮下忙

2024-12-29 19:37:14
推荐回答(1个)
回答1:

#include
#include

/*保存文件*/
FILE *pf = NULL;
void writefile(int num1,int num2)
{
fprintf(pf,"(%d,%d)\n",num1,num2);
}

/*计算一个数的真因子的和的函数,返回计算的和值*/
int jszysh(int num)
{
int rtv = 0;
int i = 2;
int tmp = 0;
for ( i = 2; i <= (int)sqrt((double)num)+1; i++ )
{
if ( num % i == 0 )
{
tmp = num / i;
if ( tmp != i )
{
rtv += (i + tmp);
}
else
{
rtv += i;
}
}
}
rtv = rtv + 1;
return rtv;
}

//此函数不需要返回值,也不需要返回数组,直接写文件就好了.
void heqinmath(int m,int n)
{
int i,j;

for ( i = m ; i < n ; i++ )
{
for ( j = i+1 ; j <= n ; j++ )
{
if ( jszysh(i) == j && jszysh(j) == i )
{
writefile(i,j);
}
}
}
}

int main(void)
{
int m,n;

printf("输入区间m和n:\n");
printf(" m = ");
scanf("%d",&m);
printf(" n = ");
scanf("%d",&n);

pf = fopen("myf2.out","wt");
if ( NULL == pf )
{
perror("打开文件失败!");
return 0;
}

heqinmath(m,n);

fclose(pf);

return 0;
}