📌  相关文章
📜  将数字分为三部分

📅  最后修改于: 2021-04-24 03:33:17             🧑  作者: Mango

给定一个非常大的数字,请将其分成3个整数,以求它们加和成原来的数字,并计算完成此操作的数量。

例子 :

Input : 3
Output : 10
The possible combinations where the sum
of the numbers is equal to 3 are:
0+0+3 = 3
0+3+0 = 3
3+0+0 = 3
0+1+2 = 3
0+2+1 = 3
1+0+2 = 3
1+2+0 = 3
2+0+1 = 3
2+1+0 = 3
1+1+1 = 3

Input : 6
Output : 28

共有10种方法,所以答案是10。

幼稚的方法:尝试从0到给定数字的所有组合,并检查它们是否等于给定数字,如果确实如此,则将计数加1并继续执行该过程。

C/C++
// C++ program to count number of ways to break
// a number in three parts.
#include 
#define ll long long int
using namespace std;
  
// Function to count number of ways
// to make the given number n
ll count_of_ways(ll n)
{
    ll count = 0;
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= n; j++)
            for (int k = 0; k <= n; k++)
                if (i + j + k == n)
                    count++;
    return count;
}
  
// Driver Function
int main()
{
    ll n = 3;
    cout << count_of_ways(n) << endl;
    return 0;
}


Java
// Java program to count number of ways to break
// a number in three parts
import java.io.*;
  
class GFG {
    // Function to count number of ways
    // to make the given number n
    static long count_of_ways(long n)
    {
        long count = 0;
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= n; j++)
                for (int k = 0; k <= n; k++)
                    if (i + j + k == n)
                        count++;
        return count;
    }
  
    // driver program
    public static void main(String[] args)
    {
        long n = 3;
        System.out.println(count_of_ways(n));
    }
}
  
// Contributed by Pramod Kumar


Python3
# Python3 program to count number of 
# ways to break
# a number in three parts.
  
# Function to count number of ways
# to make the given number n
def count_of_ways(n):
  
    count = 0
    for i in range(0, n+1):
        for j in range(0, n+1):
            for k in range(0, n+1):
                if(i + j + k == n):
                    count = count + 1
    return count
  
# Driver Function
if __name__=='__main__':
    n = 3
    print(count_of_ways(n))
  
  
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to count number of ways 
// to break a number in three parts
using System;
  
class GFG {
      
    // Function to count number of ways
    // to make the given number n
    static long count_of_ways(long n)
    {
        long count = 0;
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= n; j++)
                for (int k = 0; k <= n; k++)
                    if (i + j + k == n)
                        count++;
        return count;
    }
  
    // driver program
    public static void Main()
    {
        long n = 3;
        Console.WriteLine(count_of_ways(n));
    }
}
  
// This code is Contributed by vt_m.


PHP


C/C++
// C++ program to count number of ways to break
// a number in three parts.
#include 
#define ll long long int
using namespace std;
  
// Function to count number of ways
// to make the given number n
ll count_of_ways(ll n)
{
    ll count;
    count = (n + 1) * (n + 2) / 2;
    return count;
}
  
// Driver Function
int main()
{
    ll n = 3;
    cout << count_of_ways(n) << endl;
    return 0;
}


Java
// Java program to count number of ways to break
// a number in three parts
import java.io.*;
  
class GFG {
    // Function to count number of ways
    // to make the given number n
    static long count_of_ways(long n)
    {
        long count = 0;
        count = (n + 1) * (n + 2) / 2;
        return count;
    }
  
    // driver program
    public static void main(String[] args)
    {
        long n = 3;
        System.out.println(count_of_ways(n));
    }
}
  
// Contributed by Pramod Kumar


Python3
# Python 3 program to count number of 
# ways to break a number in three parts.
  
# Function to count number of ways
# to make the given number n
def count_of_ways(n):
    count = 0
    count = (n + 1) * (n + 2) // 2
    return count
  
# Driver code
n = 3
print(count_of_ways(n))
  
# This code is contributed by Shrikant13


C#
// C# program to count number of ways to
// break a number in three parts
using System;
  
class GFG {
      
    // Function to count number of ways
    // to make the given number n
    static long count_of_ways(long n)
    {
        long count = 0;
        count = (n + 1) * (n + 2) / 2;
        return count;
    }
  
    // driver program
    public static void Main()
    {
        long n = 3;
        Console.WriteLine(count_of_ways(n));
    }
}
  
// This code is Contributed by vt_m.


PHP


输出 :

10

时间复杂度: O(n 3 )

高效的方法:如果我们仔细观察测试用例,就会发现将n分解为3个部分的方法的数量等于(n + 1)*(n + 2)/ 2。

C / C++

// C++ program to count number of ways to break
// a number in three parts.
#include 
#define ll long long int
using namespace std;
  
// Function to count number of ways
// to make the given number n
ll count_of_ways(ll n)
{
    ll count;
    count = (n + 1) * (n + 2) / 2;
    return count;
}
  
// Driver Function
int main()
{
    ll n = 3;
    cout << count_of_ways(n) << endl;
    return 0;
}

Java

// Java program to count number of ways to break
// a number in three parts
import java.io.*;
  
class GFG {
    // Function to count number of ways
    // to make the given number n
    static long count_of_ways(long n)
    {
        long count = 0;
        count = (n + 1) * (n + 2) / 2;
        return count;
    }
  
    // driver program
    public static void main(String[] args)
    {
        long n = 3;
        System.out.println(count_of_ways(n));
    }
}
  
// Contributed by Pramod Kumar

Python3

# Python 3 program to count number of 
# ways to break a number in three parts.
  
# Function to count number of ways
# to make the given number n
def count_of_ways(n):
    count = 0
    count = (n + 1) * (n + 2) // 2
    return count
  
# Driver code
n = 3
print(count_of_ways(n))
  
# This code is contributed by Shrikant13

C#

// C# program to count number of ways to
// break a number in three parts
using System;
  
class GFG {
      
    // Function to count number of ways
    // to make the given number n
    static long count_of_ways(long n)
    {
        long count = 0;
        count = (n + 1) * (n + 2) / 2;
        return count;
    }
  
    // driver program
    public static void Main()
    {
        long n = 3;
        Console.WriteLine(count_of_ways(n));
    }
}
  
// This code is Contributed by vt_m.

的PHP


输出 :

10

时间复杂度: O(1)