📌  相关文章
📜  计算三元组(a,b,c),以使a + b,b + c和a + c都可被K整除

📅  最后修改于: 2021-05-07 08:41:38             🧑  作者: Mango

给定两个整数“ N”和“ K”,任务是计算不大于“ N”的正整数的三元组(a,b,c)的数量,以使“ a + b”,“ b + c”和“ “ c + a”都是“ K”的倍数。请注意,三元组中的“ a”,“ b”和“ c”可能相同,也可能不同。
例子:

方法:从’1’到’N’运行三个嵌套循环,并检查i + j,j + 1和l + i是否都可被’K’整除。如果条件为真,则增加计数。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
class gfg
{
    // Function returns the
    // count of the triplets
    public:
    long count_triples(int n, int k);
};
  
    long gfg :: count_triples(int n, int k)
    {
        int i = 0, j = 0, l = 0;
        int count = 0;
 
        // iterate for all
        // triples pairs (i, j, l)
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= n; j++)
            {
                for (l = 1; l <= n; l++)
                {
 
                    // if the condition
                    // is satisfied
                    if ((i + j) % k == 0
                        && (i + l) % k == 0
                        && (j + l) % k == 0)
                        count++;
                }
            }
        }
        return count;
    }
 
    // Driver code
    int main()
    {
        gfg g;
        int n = 3;
        int k = 2;
        long ans = g.count_triples(n, k);
        cout << ans;
    }
//This code is contributed by Soumik


Java
// Java implementation of the approach
class GFG {
 
    // Function returns the
    // count of the triplets
    static long count_triples(int n, int k)
    {
        int i = 0, j = 0, l = 0;
        int count = 0;
 
        // iterate for all
        // triples pairs (i, j, l)
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= n; j++) {
                for (l = 1; l <= n; l++) {
 
                    // if the condition
                    // is satisfied
                    if ((i + j) % k == 0
                        && (i + l) % k == 0
                        && (j + l) % k == 0)
                        count++;
                }
            }
        }
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 3;
        int k = 2;
        long ans = count_triples(n, k);
        System.out.println(ans);
    }
}


Python3
# Python3 implementation of the
# above approach
def count_triples(n, k):
     
    count, i, j, l = 0, 0, 0, 0
 
    # Iterate for all triples
    # pairs (i, j, l)
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            for l in range(1, n + 1):
                 
                # If the condition
                # is satisfied
                if ((i + j) % k == 0 and
                    (i + l) % k == 0 and
                    (j + l) % k == 0):
                    count += 1
         
    return count
 
# Driver code
if __name__ == "__main__":
     
    n, k = 3, 2
    ans = count_triples(n, k)
    print(ans)
     
# This code is contributed
# by Rituraj Jain


C#
// C# implementation of the approach
 
using System;
 
class GFG {
  
    // Function returns the
    // count of the triplets
    static long count_triples(int n, int k)
    {
        int i = 0, j = 0, l = 0;
        int count = 0;
  
        // iterate for all
        // triples pairs (i, j, l)
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= n; j++) {
                for (l = 1; l <= n; l++) {
  
                    // if the condition
                    // is satisfied
                    if ((i + j) % k == 0
                        && (i + l) % k == 0
                        && (j + l) % k == 0)
                        count++;
                }
            }
        }
        return count;
    }
  
    // Driver code
    public static void Main()
    {
        int n = 3;
        int k = 2;
        long ans = count_triples(n, k);
        Console.WriteLine(ans);
    }
}


PHP


Javascript


输出:
9