📌  相关文章
📜  友好数对

📅  最后修改于: 2021-10-27 08:34:51             🧑  作者: Mango

给定一个整数数组,打印数组中形成友好对的对数。如果第一个数等于第二个的除数之和,并且第二个数等于第一个的除数之和,则两个数是友好的。
例子 :

Input  : arr[] = {220, 284, 1184, 1210, 2, 5}
Output : 2
Explanation : (220, 284) and (1184, 1210) 
              form amicable pair

Input  : arr[] = {2620, 2924, 5020, 5564, 6232, 6368}
Output : 3
Explanation : (2620, 2924), (5020, 5564) and (6232, 6368)
              forms amicable pair

一个简单的解决方案是遍历每一对并检查它们是否形成友好对,如果它们形成我们增加计数。

C++
// A simple C++ program to count
// amicable pairs in an array.
#include 
using namespace std;
 
// Calculate the sum
// of proper divisors
int sumOfDiv(int x)
{
    // 1 is a proper divisor
    int sum = 1;
    for (int i = 2; i <= sqrt(x); i++)
    {
        if (x % i == 0)
        {
            sum += i;
 
            // To handle perfect squares
            if (x / i != i)
                sum += x / i;
        }
    }
    return sum;
}
 
// Check if pair is amicable
bool isAmicable(int a, int b)
{
    return (sumOfDiv(a) == b &&
            sumOfDiv(b) == a);
}
 
// This function prints pair
// of amicable pairs present
// in the input array
int countPairs(int arr[], int n)
{
    int count = 0;
 
    // Iterate through each
    // pair, and find if it
    // an amicable pair
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (isAmicable(arr[i], arr[j]))
                count++;
 
    return count;
}
 
// Driver code
int main()
{
    int arr1[] = { 220, 284, 1184,
                   1210, 2, 5 };
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    cout << countPairs(arr1, n1)
         << endl;
 
    int arr2[] = { 2620, 2924, 5020,
                   5564, 6232, 6368 };
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    cout << countPairs(arr2, n2);
    return 0;
}


Java
// A simple Java program to count
// amicable pairs in an array.
import java.io.*;
 
class GFG
{
 
    // Calculate the sum
    // of proper divisors
    static int sumOfDiv(int x)
    {
 
        // 1 is a proper divisor
        int sum = 1;
        for (int i = 2; i <= Math.sqrt(x); i++)
        {
            if (x % i == 0)
            {
                sum += i;
 
                // To handle perfect squares
                if (x / i != i)
                    sum += x / i;
            }
        }
 
        return sum;
    }
 
    // Check if pair is amicable
    static boolean isAmicable(int a, int b)
    {
        return (sumOfDiv(a) == b &&
                sumOfDiv(b) == a);
    }
 
    // This function prints pair
    //  of amicable pairs present
    // in the input array
    static int countPairs(int arr[], int n)
    {
        int count = 0;
 
        // Iterate through each pair,
        // and find if it an amicable pair
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
                if (isAmicable(arr[i], arr[j]))
                    count++;
 
        return count;
    }
 
    // Driver code
    public static void main(String args[])
    {
 
        int arr1[] = { 220, 284, 1184,
                       1210, 2, 5 };
        int n1 = arr1.length;
 
        System.out.println(countPairs(arr1, n1));
 
        int arr2[] = { 2620, 2924, 5020,
                       5564, 6232, 6368 };
        int n2 = arr2.length;
 
        System.out.println(countPairs(arr2, n2));
    }
}
 
// This code is contributed by Anshika Goyal.


Python3
# Python3 program to count
# amicable pairs in an array
 
# Calculate the sum
# of proper divisors
def sumOfDiv(x):
    sum = 1
    for i in range(2, x):
        if x % i == 0:
            sum += i
    return sum
 
# Check if pair is amicable
def isAmicable(a, b):
    if sumOfDiv(a) == b and sumOfDiv(b) == a:
        return True
    else:
        return False
 
# This function prints pair
# of amicable pairs present
# in the input array
def countPairs(arr, n):
    count = 0
    for i in range(0, n):
        for j in range(i + 1, n):
            if isAmicable(arr[i], arr[j]):
                count = count + 1
    return count
 
# Driver Code
arr1 = [220, 284, 1184,
        1210, 2, 5]
n1 = len(arr1)
print(countPairs(arr1, n1))
 
arr2 = [2620, 2924, 5020,
        5564, 6232, 6368]
n2 = len(arr2)
print(countPairs(arr2, n2))
 
# This code is contributed
# by Smitha Dinesh Semwal


C#
// A simple C# program to count
// amicable pairs in an array.
using System;
 
class GFG
{
     
    // Calculate the sum
    // of proper divisors
    static int sumOfDiv(int x)
    {
         
        // 1 is a proper divisor
        int sum = 1;
        for (int i = 2; i <= Math.Sqrt(x); i++)
        {
            if (x % i == 0)
            {
                sum += i;
 
                // To handle perfect squares
                if (x / i != i)
                    sum += x / i;
            }
        }
         
        return sum;
    }
 
    // Check if pair is amicable
    static bool isAmicable(int a, int b)
    {
        return (sumOfDiv(a) == b &&
                sumOfDiv(b) == a);
    }
 
    // This function prints pair
    // of amicable pairs present
    // in the input array
    static int countPairs(int []arr, int n)
    {
        int count = 0;
 
        // Iterate through each pair,
        // and find if it an amicable pair
        for (int i = 0; i < n; i++)
         
            for (int j = i + 1; j < n; j++)
                if (isAmicable(arr[i], arr[j]))
                    count++;
 
        return count;
    }
 
    // Driver code
    public static void Main()
    {
 
        int []arr1 = {220, 284, 1184,
                      1210, 2, 5};
        int n1 = arr1.Length;
         
        Console.WriteLine(countPairs(arr1, n1));
 
        int []arr2 = {2620, 2924, 5020,
                      5564, 6232, 6368};
        int n2 = arr2.Length;
 
        Console.WriteLine(countPairs(arr2, n2));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// Efficient C++ program to count
// Amicable pairs in an array.
#include 
using namespace std;
 
// Calculate the sum
// of proper divisors
int sumOfDiv(int x)
{
    // 1 is a proper divisor
    int sum = 1;
    for (int i = 2; i <= sqrt(x); i++)
    {
        if (x % i == 0) {
            sum += i;
 
            // To handle perfect squares
            if (x / i != i)
                sum += x / i;
        }
    }
    return sum;
}
 
// Check if pair is amicable
bool isAmicable(int a, int b)
{
    return (sumOfDiv(a) == b &&
            sumOfDiv(b) == a);
}
 
// This function prints count
// of amicable pairs present
// in the input array
int countPairs(int arr[], int n)
{
    // Map to store the numbers
    unordered_set s;
    int count = 0;
    for (int i = 0; i < n; i++)
        s.insert(arr[i]);
 
    // Iterate through each number,
    // and find the sum of proper
    // divisors and check if it's
    // also present in the array
    for (int i = 0; i < n; i++)
    {
        if (s.find(sumOfDiv(arr[i])) != s.end())
        {
            // It's sum of proper divisors
            int sum = sumOfDiv(arr[i]);
            if (isAmicable(arr[i], sum))
                count++;
        }
    }
 
    // As the pairs are counted
    // twice, thus divide by 2
    return count / 2;
}
 
// Driver code
int main()
{
    int arr1[] = { 220, 284, 1184,
                   1210, 2, 5 };
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    cout << countPairs(arr1, n1)
         << endl;
     
    int arr2[] = { 2620, 2924, 5020,
                   5564, 6232, 6368 };
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    cout << countPairs(arr2, n2)
         << endl;
    return 0;
}


Java
// Efficient Java program to count
// Amicable pairs in an array.
import java.util.*;
 
class GFG
{
 
// Calculate the sum
// of proper divisors
static int sumOfDiv(int x)
{
    // 1 is a proper divisor
    int sum = 1;
    for (int i = 2; i <= Math.sqrt(x); i++)
    {
        if (x % i == 0)
        {
            sum += i;
 
            // To handle perfect squares
            if (x / i != i)
                sum += x / i;
        }
    }
    return sum;
}
 
// Check if pair is amicable
static boolean isAmicable(int a, int b)
{
    return (sumOfDiv(a) == b &&
            sumOfDiv(b) == a);
}
 
// This function prints count
// of amicable pairs present
// in the input array
static int countPairs(int arr[], int n)
{
    // Map to store the numbers
    HashSet s = new HashSet();
    int count = 0;
    for (int i = 0; i < n; i++)
        s.add(arr[i]);
 
    // Iterate through each number,
    // and find the sum of proper
    // divisors and check if it's
    // also present in the array
    for (int i = 0; i < n; i++)
    {
        if (s.contains(sumOfDiv(arr[i])))
        {
            // It's sum of proper divisors
            int sum = sumOfDiv(arr[i]);
            if (isAmicable(arr[i], sum))
                count++;
        }
    }
 
    // As the pairs are counted
    // twice, thus divide by 2
    return count / 2;
}
 
// Driver code
public static void main(String[] args)
{
    int arr1[] = { 220, 284, 1184,
                   1210, 2, 5 };
    int n1 = arr1.length;
    System.out.println(countPairs(arr1, n1));
     
    int arr2[] = { 2620, 2924, 5020,
                   5564, 6232, 6368 };
    int n2 = arr2.length;
    System.out.println(countPairs(arr2, n2));
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Efficient Python3 program to count
# Amicable pairs in an array.
import math
 
# Calculating the sum
# of proper divisors
def sumOfDiv(x):
     
    # 1 is a proper divisor
    sum = 1;
    for i in range(2,int(math.sqrt(x))):
        if x % i==0:
            sum += i
             
            # To handle perfect squares
            if i != x/i:
                sum += x/i
    return int(sum);
 
# check if pair is ambicle
def isAmbicle(a, b):
    return (sumOfDiv(a) == b and
            sumOfDiv(b) == a)
 
# This function prints count
# of amicable pairs present
# in the input array
def countPairs(arr,n):
     
    # Map to store the numbers
    s = set()
    count = 0
    for i in range(n):
        s.add(arr[i])
     
    # Iterate through each number,
    # and find the sum of proper
    # divisors and check if it's
    # also present in the array
    for i in range(n):    
        if sumOfDiv(arr[i]) in s:
             
            # It's sum of proper divisors
            sum = sumOfDiv(arr[i])
            if isAmbicle(arr[i], sum):
                count += 1       
     
    # As the pairs are counted
    # twice, thus divide by 2
    return int(count/2);
     
# Driver Code
arr1 = [220, 284, 1184,
        1210, 2, 5]
n1 = len(arr1)
print(countPairs(arr1, n1))
 
arr2 = [2620, 2924, 5020,
        5564, 6232, 6368]
n2 = len(arr2)
print(countPairs(arr2, n2))
 
# This code is contributed
# by Naveen Babbar


C#
// Efficient C# program to count
// Amicable pairs in an array.
using System;
using System.Collections.Generic;
     
class GFG
{
 
// Calculate the sum
// of proper divisors
static int sumOfDiv(int x)
{
    // 1 is a proper divisor
    int sum = 1;
    for (int i = 2; i <= Math.Sqrt(x); i++)
    {
        if (x % i == 0)
        {
            sum += i;
 
            // To handle perfect squares
            if (x / i != i)
                sum += x / i;
        }
    }
    return sum;
}
 
// Check if pair is amicable
static Boolean isAmicable(int a, int b)
{
    return (sumOfDiv(a) == b &&
            sumOfDiv(b) == a);
}
 
// This function prints count
// of amicable pairs present
// in the input array
static int countPairs(int []arr, int n)
{
    // Map to store the numbers
    HashSet s = new HashSet();
    int count = 0;
    for (int i = 0; i < n; i++)
        s.Add(arr[i]);
 
    // Iterate through each number,
    // and find the sum of proper
    // divisors and check if it's
    // also present in the array
    for (int i = 0; i < n; i++)
    {
        if (s.Contains(sumOfDiv(arr[i])))
        {
            // It's sum of proper divisors
            int sum = sumOfDiv(arr[i]);
            if (isAmicable(arr[i], sum))
                count++;
        }
    }
 
    // As the pairs are counted
    // twice, thus divide by 2
    return count / 2;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr1 = { 220, 284, 1184,
                   1210, 2, 5 };
    int n1 = arr1.Length;
    Console.WriteLine(countPairs(arr1, n1));
     
    int []arr2 = { 2620, 2924, 5020,
                   5564, 6232, 6368 };
    int n2 = arr2.Length;
    Console.WriteLine(countPairs(arr2, n2));
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:

2
3

一个有效的解决方案是将数字存储在映射中,对于每个数字,我们找到其适当除数的总和并检查它是否也存在于数组中。如果存在,我们可以检查它们是否形成友好的对。
因此,复杂性将大大降低。下面是相同的 C++ 程序。

C++

// Efficient C++ program to count
// Amicable pairs in an array.
#include 
using namespace std;
 
// Calculate the sum
// of proper divisors
int sumOfDiv(int x)
{
    // 1 is a proper divisor
    int sum = 1;
    for (int i = 2; i <= sqrt(x); i++)
    {
        if (x % i == 0) {
            sum += i;
 
            // To handle perfect squares
            if (x / i != i)
                sum += x / i;
        }
    }
    return sum;
}
 
// Check if pair is amicable
bool isAmicable(int a, int b)
{
    return (sumOfDiv(a) == b &&
            sumOfDiv(b) == a);
}
 
// This function prints count
// of amicable pairs present
// in the input array
int countPairs(int arr[], int n)
{
    // Map to store the numbers
    unordered_set s;
    int count = 0;
    for (int i = 0; i < n; i++)
        s.insert(arr[i]);
 
    // Iterate through each number,
    // and find the sum of proper
    // divisors and check if it's
    // also present in the array
    for (int i = 0; i < n; i++)
    {
        if (s.find(sumOfDiv(arr[i])) != s.end())
        {
            // It's sum of proper divisors
            int sum = sumOfDiv(arr[i]);
            if (isAmicable(arr[i], sum))
                count++;
        }
    }
 
    // As the pairs are counted
    // twice, thus divide by 2
    return count / 2;
}
 
// Driver code
int main()
{
    int arr1[] = { 220, 284, 1184,
                   1210, 2, 5 };
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    cout << countPairs(arr1, n1)
         << endl;
     
    int arr2[] = { 2620, 2924, 5020,
                   5564, 6232, 6368 };
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    cout << countPairs(arr2, n2)
         << endl;
    return 0;
}

Java

// Efficient Java program to count
// Amicable pairs in an array.
import java.util.*;
 
class GFG
{
 
// Calculate the sum
// of proper divisors
static int sumOfDiv(int x)
{
    // 1 is a proper divisor
    int sum = 1;
    for (int i = 2; i <= Math.sqrt(x); i++)
    {
        if (x % i == 0)
        {
            sum += i;
 
            // To handle perfect squares
            if (x / i != i)
                sum += x / i;
        }
    }
    return sum;
}
 
// Check if pair is amicable
static boolean isAmicable(int a, int b)
{
    return (sumOfDiv(a) == b &&
            sumOfDiv(b) == a);
}
 
// This function prints count
// of amicable pairs present
// in the input array
static int countPairs(int arr[], int n)
{
    // Map to store the numbers
    HashSet s = new HashSet();
    int count = 0;
    for (int i = 0; i < n; i++)
        s.add(arr[i]);
 
    // Iterate through each number,
    // and find the sum of proper
    // divisors and check if it's
    // also present in the array
    for (int i = 0; i < n; i++)
    {
        if (s.contains(sumOfDiv(arr[i])))
        {
            // It's sum of proper divisors
            int sum = sumOfDiv(arr[i]);
            if (isAmicable(arr[i], sum))
                count++;
        }
    }
 
    // As the pairs are counted
    // twice, thus divide by 2
    return count / 2;
}
 
// Driver code
public static void main(String[] args)
{
    int arr1[] = { 220, 284, 1184,
                   1210, 2, 5 };
    int n1 = arr1.length;
    System.out.println(countPairs(arr1, n1));
     
    int arr2[] = { 2620, 2924, 5020,
                   5564, 6232, 6368 };
    int n2 = arr2.length;
    System.out.println(countPairs(arr2, n2));
}
}
 
// This code is contributed by PrinciRaj1992

蟒蛇3

# Efficient Python3 program to count
# Amicable pairs in an array.
import math
 
# Calculating the sum
# of proper divisors
def sumOfDiv(x):
     
    # 1 is a proper divisor
    sum = 1;
    for i in range(2,int(math.sqrt(x))):
        if x % i==0:
            sum += i
             
            # To handle perfect squares
            if i != x/i:
                sum += x/i
    return int(sum);
 
# check if pair is ambicle
def isAmbicle(a, b):
    return (sumOfDiv(a) == b and
            sumOfDiv(b) == a)
 
# This function prints count
# of amicable pairs present
# in the input array
def countPairs(arr,n):
     
    # Map to store the numbers
    s = set()
    count = 0
    for i in range(n):
        s.add(arr[i])
     
    # Iterate through each number,
    # and find the sum of proper
    # divisors and check if it's
    # also present in the array
    for i in range(n):    
        if sumOfDiv(arr[i]) in s:
             
            # It's sum of proper divisors
            sum = sumOfDiv(arr[i])
            if isAmbicle(arr[i], sum):
                count += 1       
     
    # As the pairs are counted
    # twice, thus divide by 2
    return int(count/2);
     
# Driver Code
arr1 = [220, 284, 1184,
        1210, 2, 5]
n1 = len(arr1)
print(countPairs(arr1, n1))
 
arr2 = [2620, 2924, 5020,
        5564, 6232, 6368]
n2 = len(arr2)
print(countPairs(arr2, n2))
 
# This code is contributed
# by Naveen Babbar

C#

// Efficient C# program to count
// Amicable pairs in an array.
using System;
using System.Collections.Generic;
     
class GFG
{
 
// Calculate the sum
// of proper divisors
static int sumOfDiv(int x)
{
    // 1 is a proper divisor
    int sum = 1;
    for (int i = 2; i <= Math.Sqrt(x); i++)
    {
        if (x % i == 0)
        {
            sum += i;
 
            // To handle perfect squares
            if (x / i != i)
                sum += x / i;
        }
    }
    return sum;
}
 
// Check if pair is amicable
static Boolean isAmicable(int a, int b)
{
    return (sumOfDiv(a) == b &&
            sumOfDiv(b) == a);
}
 
// This function prints count
// of amicable pairs present
// in the input array
static int countPairs(int []arr, int n)
{
    // Map to store the numbers
    HashSet s = new HashSet();
    int count = 0;
    for (int i = 0; i < n; i++)
        s.Add(arr[i]);
 
    // Iterate through each number,
    // and find the sum of proper
    // divisors and check if it's
    // also present in the array
    for (int i = 0; i < n; i++)
    {
        if (s.Contains(sumOfDiv(arr[i])))
        {
            // It's sum of proper divisors
            int sum = sumOfDiv(arr[i]);
            if (isAmicable(arr[i], sum))
                count++;
        }
    }
 
    // As the pairs are counted
    // twice, thus divide by 2
    return count / 2;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr1 = { 220, 284, 1184,
                   1210, 2, 5 };
    int n1 = arr1.Length;
    Console.WriteLine(countPairs(arr1, n1));
     
    int []arr2 = { 2620, 2924, 5020,
                   5564, 6232, 6368 };
    int n2 = arr2.Length;
    Console.WriteLine(countPairs(arr2, n2));
}
}
 
// This code is contributed by Princi Singh

Javascript


输出:

2
3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程