📌  相关文章
📜  N可以表示为两个正整数之和的方式的数目

📅  最后修改于: 2021-04-23 21:44:21             🧑  作者: Mango

给定数字N ,任务是找到可以将N表示为两个正整数之和的唯一方式的数量。
例子:

方法:用两个正整数之和表示数字的方式是1 +(N – 1),2 +(N – 2),…,(N – 1)+ 1(N – 2)+ 2 。系列中有N – 1个术语,它们成对出现,即(X + Y,Y + X) 。因此所需的计数为N / 2
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the number of
// distinct ways to represent n
// as the sum of two integers
int ways(int n)
{
    return n / 2;
}
 
// Driver code
int main()
{
    int n = 2;
 
    cout << ways(n);
 
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
     
    // Function to return the number of
    // distinct ways to represent n
    // as the sum of two integers
    static int ways(int n)
    {
        return n / 2;
    }
     
    // Driver code
    public static void main(String args[])
    {
        int n = 2;
     
        System.out.println(ways(n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# Function to return the number of
# distinct ways to represent n
# as the sum of two integers
def ways(n):
    return n // 2
 
# Driver code
n = 2
 
print(ways(n))
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the number of
// distinct ways to represent n
// as the sum of two integers
static int ways(int n)
{
    return n / 2;
}
 
// Driver code
public static void Main()
{
    int n = 2;
 
    Console.WriteLine(ways(n));
}
}
 
// This code is contributed by Nidhi_Biet


Javascript


输出:
1