给定n,则打印总计为n的最大复合数。前几个复合数字为4、6、8、9、10、12、14、15、16、18、20,…………
例子:
Input: 90
Output: 22
Explanation: If we add 21 4's, then we
get 84 and then add 6 to it, we get 90.
Input: 10
Output: 2
Explanation: 4 + 6 = 10
以下是一些重要的观察。
- 如果数字小于4,则不会有任何组合。
- 如果数字为5、7、11,则不会拆分。
- 由于最小的合成数为4,因此使用最大的4s是有意义的。
- 对于在除以4时不会留下复合余数的数字,我们进行以下操作。如果余数是1,我们从中减去9,得到可以被4整除的数。如果余数是2,则从中减去6,得到可以被4整除的na数。如果余数是3,那么从中减去15可以使n完全被4整除,而15可以由9 + 6组成。
因此,主要的观察结果是使n构成最大为4的数,其余的数可以由6和9组成。我们将不需要复合数,因为可以构成9以上的复合数。 4、6和9。
下面是上述方法的实现
C++
// CPP program to split a number into maximum
// number of composite numbers.
#include
using namespace std;
// function to calculate the maximum number of
// composite numbers adding upto n
int count(int n)
{
// 4 is the smallest composite number
if (n < 4)
return -1;
// stores the remainder when n is divided
// by 4
int rem = n % 4;
// if remainder is 0, then it is perfectly
// divisible by 4.
if (rem == 0)
return n / 4;
// if the remainder is 1
if (rem == 1) {
// If the number is less then 9, that
// is 5, then it cannot be expressed as
// 4 is the only composite number less
// than 5
if (n < 9)
return -1;
// If the number is greater then 8, and
// has a remainder of 1, then express n
// as n-9 a and it is perfectly divisible
// by 4 and for 9, count 1.
return (n - 9) / 4 + 1;
}
// When remainder is 2, just subtract 6 from n,
// so that n is perfectly divisible by 4 and
// count 1 for 6 which is subtracted.
if (rem == 2)
return (n - 6) / 4 + 1;
// if the number is 7, 11 which cannot be
// expressed as sum of any composite numbers
if (rem == 3) {
if (n < 15)
return -1;
// when the remainder is 3, then subtract
// 15 from it and n becomes perfectly
// divisible by 4 and we add 2 for 9 and 6,
// which is getting subtracted to make n
// perfectly divisible by 4.
return (n - 15) / 4 + 2;
}
}
// driver program to test the above function
int main()
{
int n = 90;
cout << count(n) << endl;
n = 143;
cout << count(n) << endl;
return 0;
}
Java
// Java program to split a number into maximum
// number of composite numbers.
import java.io.*;
class GFG
{
// function to calculate the maximum number of
// composite numbers adding upto n
static int count(int n)
{
// 4 is the smallest composite number
if (n < 4)
return -1;
// stores the remainder when n is divided
// by 4
int rem = n % 4;
// if remainder is 0, then it is perfectly
// divisible by 4.
if (rem == 0)
return n / 4;
// if the remainder is 1
if (rem == 1) {
// If the number is less then 9, that
// is 5, then it cannot be expressed as
// 4 is the only composite number less
// than 5
if (n < 9)
return -1;
// If the number is greater then 8, and
// has a remainder of 1, then express n
// as n-9 a and it is perfectly divisible
// by 4 and for 9, count 1.
return (n - 9) / 4 + 1;
}
// When remainder is 2, just subtract 6 from n,
// so that n is perfectly divisible by 4 and
// count 1 for 6 which is subtracted.
if (rem == 2)
return (n - 6) / 4 + 1;
// if the number is 7, 11 which cannot be
// expressed as sum of any composite numbers
if (rem == 3)
{
if (n < 15)
return -1;
// when the remainder is 3, then subtract
// 15 from it and n becomes perfectly
// divisible by 4 and we add 2 for 9 and 6,
// which is getting subtracted to make n
// perfectly divisible by 4.
return (n - 15) / 4 + 2;
}
return 0;
}
// Driver program
public static void main (String[] args)
{
int n = 90;
System.out.println(count(n));
n = 143;
System.out.println(count(n));
}
}
// This code is contributed by vt_m.
Python3
# Python3 program to split a number into
# maximum number of composite numbers.
# Function to calculate the maximum number
# of composite numbers adding upto n
def count(n):
# 4 is the smallest composite number
if (n < 4):
return -1
# stores the remainder when n
# is divided n is divided by 4
rem = n % 4
# if remainder is 0, then it is
# perfectly divisible by 4.
if (rem == 0):
return n // 4
# if the remainder is 1
if (rem == 1):
# If the number is less then 9, that
# is 5, then it cannot be expressed as
# 4 is the only composite number less
# than 5
if (n < 9):
return -1
# If the number is greater then 8, and
# has a remainder of 1, then express n
# as n-9 a and it is perfectly divisible
# by 4 and for 9, count 1.
return (n - 9) // 4 + 1
# When remainder is 2, just subtract 6 from n,
# so that n is perfectly divisible by 4 and
# count 1 for 6 which is subtracted.
if (rem == 2):
return (n - 6) // 4 + 1
# if the number is 7, 11 which cannot be
# expressed as sum of any composite numbers
if (rem == 3):
if (n < 15):
return -1
# when the remainder is 3, then subtract
# 15 from it and n becomes perfectly
# divisible by 4 and we add 2 for 9 and 6,
# which is getting subtracted to make n
# perfectly divisible by 4.
return (n - 15) // 4 + 2
# Driver Code
n = 90
print(count(n))
n = 143
print(count(n))
# This code is contributed by Anant Agarwal.
C#
// C# program to split a number into maximum
// number of composite numbers.
using System;
class GFG {
// function to calculate the maximum number
// of composite numbers adding upto n
static int count(int n)
{
// 4 is the smallest composite number
if (n < 4)
return -1;
// stores the remainder when n is divided
// by 4
int rem = n % 4;
// if remainder is 0, then it is perfectly
// divisible by 4.
if (rem == 0)
return n / 4;
// if the remainder is 1
if (rem == 1) {
// If the number is less then 9, that
// is 5, then it cannot be expressed as
// 4 is the only composite number less
// than 5
if (n < 9)
return -1;
// If the number is greater then 8, and
// has a remainder of 1, then express n
// as n-9 a and it is perfectly divisible
// by 4 and for 9, count 1.
return (n - 9) / 4 + 1;
}
// When remainder is 2, just subtract 6 from n,
// so that n is perfectly divisible by 4 and
// count 1 for 6 which is subtracted.
if (rem == 2)
return (n - 6) / 4 + 1;
// if the number is 7, 11 which cannot be
// expressed as sum of any composite numbers
if (rem == 3)
{
if (n < 15)
return -1;
// when the remainder is 3, then subtract
// 15 from it and n becomes perfectly
// divisible by 4 and we add 2 for 9 and 6,
// which is getting subtracted to make n
// perfectly divisible by 4.
return (n - 15) / 4 + 2;
}
return 0;
}
// Driver program
public static void Main()
{
int n = 90;
Console.WriteLine(count(n));
n = 143;
Console.WriteLine(count(n));
}
}
// This code is contributed by Anant Agarwal.
PHP
输出:
22
34
时间复杂度:O(1)
辅助空间:O(1)