给定数字N。找出将数字分为四个部分(a,b,c,d)的方式数,使得a = c和b = d且a不等于b。
例子:
Input : N = 6
Output : 1
Expalantion : four parts are {1, 2, 1, 2}
Input : N = 20
Output : 4
Expalantion : possible ways are {1, 1, 9, 9}, {2, 2, 8, 8}, {3, 3, 7, 7} and {4, 4, 6, 6}.
方法 :
如果给定的N为奇数,则答案为0,因为四部分之和将不是偶数。
如果n被4整除,则答案将为n / 4 -1(此处a等于b是可能的,因此减去该可能性),否则为n / 4。
下面是上述方法的实现:
C++
// C++ implementation for above approach
#include
using namespace std;
// Function to find the number of ways to divide
// N into four parts such that a = c and b = d
int possibleways(int n)
{
if (n % 2 == 1)
return 0;
else if (n % 4 == 0)
return n / 4 - 1;
else
return n / 4;
}
// Driver code
int main()
{
int n = 20;
cout << possibleways(n);
return 0;
}
Java
// Java implementation for above approach
class GFG
{
// Function to find the number of ways to divide
// N into four parts such that a = c and b = d
static int possibleways(int n)
{
if (n % 2 == 1)
return 0;
else if (n % 4 == 0)
return n / 4 - 1;
else
return n / 4;
}
// Driver code
public static void main(String[] args)
{
int n = 20;
System.out.println(possibleways(n));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 implementation for above approach
# Function to find the number of ways
# to divide N into four parts
# such that a = c and b = d
def possibleways(n):
if (n % 2 == 1):
return 0;
elif (n % 4 == 0):
return n // 4 - 1;
else:
return n // 4;
# Driver code
n = 20;
print(possibleways(n));
# This code is contributed by mits
C#
// C# implementation for above approach
class GFG
{
// Function to find the number of ways to divide
// N into four parts such that a = c and b = d
static int possibleways(int n)
{
if (n % 2 == 1)
return 0;
else if (n % 4 == 0)
return n / 4 - 1;
else
return n / 4;
}
// Driver code
static void Main()
{
int n = 20;
System.Console.WriteLine(possibleways(n));
}
}
// This code is contributed by mits
PHP
输出:
4