做了几天的亲和数对,今天晚上终于ac了,不容易呀,下面讲一讲我的做法吧,希望能够帮助大家
题目如下:
对于每组输入,输出在该区间内的有多少对友元数字。每组输出占一行。
虽然6的因子和是6,但6不是友元数字,像这样的数字被叫做完美数字。
题目是要输出亲和数的对数,而且数的范围特别大,所以这儿我们可以考虑先打表;
打表的代码如下:
由于数组元素较多,我们先把数组元素输出到硬盘上,然后粘贴复制到数组中,进行运算
#include<iostream>
using namespace std;
int main()
{
int i,k=0;
for(i=200;i<5000000;i++) //.循环500 0000,第一个数是220,所以从200循环
{
int sum1=1;
for(int j=2;j<i/j;j++)
{
if(i%j==0)
sum1=sum1+j+i/j; //求一个数的真因子,存到sum1中,
}
int sum2=1;
for(int m=2;m<sum1/m;m++)
{
if(sum1%m==0)
{
sum2=sum2+m+sum1/m; //求sum1的真因子,存到sum2中,
}
}
FILE *fp;
if(i==sum2&&i<sum1) //判断i的真因子是否等于sum1的真因子,而且要i<sum1,为了避免不重复,比如220,284,如果不交i<sum1,就会打出一个284,220;
{
cout<<k<<" "<<i<<" "<<sum1<<endl;
k++;
fp=fopen("D:\\data.txt","ab"); // 输出到D盘,可以自己修改这一步,存到自己的文件中;
fprintf(fp,"%d,%d,\r\n",i,sum1);
fclose(fp);
}
}
}
接下来,我们就要进行计算了;
#include<iostream>
using namespace std;
int main()
{//这两个数组都是上面计算出来的,a[]表示一对亲和数中较小的,b[]表示较大的
int a[71]={
220,1184,2620,5020,6232,10744,12285,17296,63020,66928,67095,69615,79750,100485,122265,122368,141664,142310,171856,
176272,185368,196724,280540,308620,319550,356408,437456,469028,503056,522405,600392,609928,624184,635624,643336,
667964,726104,802725,879712,898216,947835,998104,1077890,1154450,1156870,1175265,1185376,1280565,1328470,
1358595,1392368,1466150,1468324,1511930,1669910,1798875,2082464,2236570,2652728,2723792,2728726,2739704,2802416,
2803580,3276856,3606850,3786904,3805264,4238984,4246130,4259750};
int b[71]={
284,1210,2924,5564,6368,10856,14595,18416,76084,66992,71145,87633,88730,124155,139815,123152,153176,168730,176336,
180848,203432,202444,365084,389924,430402,399592,455344,486178,514736,525915,669688,686072,691256,712216,652664,
783556,796696,863835,901424,980984,1125765,1043096,1099390,1189150,1292570,1438983,1286744,1340235,1483850,
1486845,1464592,1747930,1749212,1598470,2062570,1870245,2090656,2429030,2941672,2874064,3077354,2928136,2947216,
3716164,3721544,3892670,4300136,4006736,4314616,4488910,4445050};
int x,y,sum,i;
while(cin>>x>>y)
{
sum=0;
for(i=0;i<71;i++)
{
if(x<=a[i]&&b[i]<=y)
sum++;
}
cout<<sum<<endl;
}
}
原文地址:http://blog.csdn.net/u013412497/article/details/39164297