a+b+c=n的非负积分解的个数

给定一个数n,我们可以用多种方法把3个非负整数相加,使它们的和为n。 例如:

null
Input : n = 1Output : 3There are four ways to get sum 1.(1, 0, 0), (0, 1, 0) and (0, 0, 1)Input : n = 2Output : 6There are six ways to get sum 2.(2, 0, 0), (0, 2, 0), (0, 0, 2), (1, 1, 0)(1, 0, 1) and (0, 1, 1)Input : n = 3Output : 10There are ten ways to get sum 10.(3, 0, 0), (0, 3, 0), (0, 0, 3), (1, 2, 0)(1, 0, 2), (0, 1, 2), (2, 1, 0), (2, 0, 1),(0, 2, 1) and (1, 1, 1)

方法1[蛮力:O(n) 3. ) ] 一个简单的解决方案是考虑所有三元组使用三个循环。对于每个三元组,检查其和是否等于n。如果和为n,则增加解决方案的计数。 下面是实现。

C++

// A naive C++ solution to count solutions of
// a + b + c = n
#include<bits/stdc++.h>
using namespace std;
// Returns count of solutions of a + b + c = n
int countIntegralSolutions( int n)
{
// Initialize result
int result = 0;
// Consider all triplets and increment
// result whenever sum of a triplet is n.
for ( int i=0; i<=n; i++)
for ( int j=0; j<=n-i; j++)
for ( int k=0; k<=(n-i-j); k++)
if (i + j + k == n)
result++;
return result;
}
// Driver code
int main()
{
int n = 3;
cout <<  countIntegralSolutions(n);
return 0;
}


JAVA

// A naive Java solution to count
// solutions of a + b + c = n
import java.io.*;
class GFG
{
// Returns count of solutions of a + b + c = n
static int countIntegralSolutions( int n)
{
// Initialize result
int result = 0 ;
// Consider all triplets and increment
// result whenever sum of a triplet is n.
for ( int i = 0 ; i <= n; i++)
for ( int j = 0 ; j <= n - i; j++)
for ( int k = 0 ; k <= (n - i - j); k++)
if (i + j + k == n)
result++;
return result;
}
// Driver code
public static void main (String[] args)
{
int n = 3 ;
System.out.println( countIntegralSolutions(n));
}
}
// This article is contributed by vt_m


Python3

# Python3 code to count
# solutions of a + b + c = n
# Returns count of solutions
# of a + b + c = n
def countIntegralSolutions (n):
# Initialize result
result = 0
# Consider all triplets and
# increment result whenever
# sum of a triplet is n.
for i in range (n + 1 ):
for j in range (n + 1 ):
for k in range (n + 1 ):
if i + j + k = = n:
result + = 1
return result
# Driver code
n = 3
print (countIntegralSolutions(n))
# This code is contributed by "Sharad_Bhardwaj".


C#

// A naive C# solution to count
// solutions of a + b + c = n
using System;
class GFG {
// Returns count of solutions
// of a + b + c = n
static int countIntegralSolutions( int n)
{
// Initialize result
int result = 0;
// Consider all triplets and increment
// result whenever sum of a triplet is n.
for ( int i = 0; i <= n; i++)
for ( int j = 0; j <= n - i; j++)
for ( int k = 0; k <= (n - i - j); k++)
if (i + j + k == n)
result++;
return result;
}
// Driver code
public static void Main ()
{
int n = 3;
Console.Write(countIntegralSolutions(n));
}
}
// This article is contributed by Smitha.


PHP

<?php
// A naive PHP solution
// to count solutions of
// a + b + c = n
// Returns count of
// solutions of a + b + c = n
function countIntegralSolutions( $n )
{
// Initialize result
$result = 0;
// Consider all triplets and increment
// result whenever sum of a triplet is n.
for ( $i = 0; $i <= $n ; $i ++)
for ( $j = 0; $j <= $n - $i ; $j ++)
for ( $k = 0; $k <= ( $n - $i - $j ); $k ++)
if ( $i + $j + $k == $n )
$result ++;
return $result ;
}
// Driver Code
$n = 3;
echo countIntegralSolutions( $n );
// This code is contributed by anuj_67.
?>


Javascript

<script>
// Javascript program solution to count
// solutions of a + b + c = n
// Returns count of solutions of a + b + c = n
function countIntegralSolutions(n)
{
// Initialize result
let result = 0;
// Consider all triplets and increment
// result whenever sum of a triplet is n.
for (let i = 0; i <= n; i++)
for (let j = 0; j <= n - i; j++)
for (let k = 0; k <= (n - i - j); k++)
if (i + j + k == n)
result++;
return result;
}
// Driver Code
let n = 3;
document.write(countIntegralSolutions(n));
</script>


输出:

10

方法2[直接公式:O(1)] 如果我们仔细观察这个模式,我们会发现解的数量是((n+1)*(n+2))/2。这个问题相当于将n+1个相同的球(0到n)分布在三个盒子中,解决方案是 n+2 C 2. .通常,如果有m个变量(或框)和n个可能的值,则公式变为 n+m-1 C m-1 .

C++

// A naive C++ solution to count solutions of
// a + b + c = n
#include<bits/stdc++.h>
using namespace std;
// Returns count of solutions of a + b + c = n
int countIntegralSolutions( int n)
{
return ((n+1)*(n+2))/2;
}
// Driver code
int main()
{
int n = 3;
cout <<  countIntegralSolutions(n);
return 0;
}


JAVA

// Java solution to count
// solutions of a + b + c = n
import java.io.*;
class GFG
{
// Returns count of solutions
// of a + b + c = n
static int countIntegralSolutions( int n)
{
return ((n + 1 ) * (n + 2 )) / 2 ;
}
// Driver code
public static void main (String[] args)
{
int n = 3 ;
System.out.println ( countIntegralSolutions(n));
}
}
// This article is contributed by vt_m


Python3

# Python3 solution to count
# solutions of a + b + c = n
# Returns count of solutions
# of a + b + c = n
def countIntegralSolutions (n):
return int (((n + 1 ) * (n + 2 )) / 2 )
# Driver code
n = 3
print (countIntegralSolutions(n))
# This code is contributed by "Sharad_Bhardwaj".


C#

// C# solution to count
// solutions of a + b + c = n
using System;
class GFG {
// Returns count of solutions
// of a + b + c = n
static int countIntegralSolutions( int n)
{
return ((n + 1) * (n + 2)) / 2;
}
// Driver code
public static void Main (String[] args)
{
int n = 3;
Console.Write ( countIntegralSolutions(n));
}
}
// This code is contributed by parashar.


PHP

<?php
// A naive PHP solution
// to count solutions of
// a + b + c = n
// Returns count of solutions
// of a + b + c = n
function countIntegralSolutions( $n )
{
return (( $n + 1) * ( $n + 2)) / 2;
}
// Driver Code
$n = 3;
echo countIntegralSolutions( $n );
// This code is contributed by anuj_67.
?>


Javascript

<script>
// A naive JavaScript solution
// to count solutions of
// a + b + c = n
// Returns count of solutions of a + b + c = n
function countIntegralSolutions(n)
{
return Math.floor(((n+1)*(n+2))/2);
}
// Driver code
let n = 3;
document.write(countIntegralSolutions(n));
// This code is contributed by Surbhi Tyagi.
</script>


输出:

10

本文由 希瓦姆·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以写一篇文章,然后将文章邮寄给评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享