算法OJ—亲密数

亲密数(close numbers)

时限:2000ms 内存限制:10000K 总时限:2000ms

描述:

两个整数a和b,如果a的不包含自身的因子之和等于b,并且b的不包含自身的因子和等于a,且a不等于b,则称a,b为一对亲密数。
找出满足a<=10000且b<=10000的全部亲密数对。
A pair of close numbers(a and b) is defined as follows: the sum of a’s factors equals b and the sum of b’s factors equals a,but a does not equals b.Find all close numbers which are not greater than 10000.

输入:

本题无输入。
None

输出:

升序输出所有满足条件的数对,每对数字一行,小数字在前,大数字在后,用空格分隔。注意:本题要求程序效率要高,直接写成二重循环肯定超时。
Output all pair of close numbers in ascending order,and each pair occupies one line with the smaller one in front and the pair is separated by a space.

输入样例:

输出样例:

本题要求了时间的复杂度,通过分析可以将遍历的数字直接进行两次是否亲密的检验即:A生成亲密数B,B的亲密数是否为A

#include<iostream>
using namespace std;

int countFactor(int i){
    int factorA;
    int sum = 1;
    for(factorA = 2;factorA <= i/2; factorA++){
        if(i % factorA == 0){
            sum += factorA;
        }
    }
    return sum;
}

int main(){
    int i;
    int j;
    int minN;
    int maxN;
    for(i = 1; i < 10000; i++){
        j = countFactor(i);
        if(countFactor(j) == i && j != i && j > i){
            cout << i << " "<< j << endl;
        }
    }

    return 0;

}
发布了15 篇原创文章 · 获赞 15 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/gcoder_/article/details/82734742
今日推荐