📌  相关文章
📜  将1到n分成和差最小的两组

📅  最后修改于: 2021-10-26 05:40:42             🧑  作者: Mango

给定一个正整数 n,使得 n > 2。将 1 到 n 的数字分成两组,使得每组总和的绝对差最小。在第一行和下一行打印该组的任何两个组的大小。
例子:

Input : 5
Output : 2
         5 2
         3
         4 3 1
Here sum of group 1 is 7 and sum of group 2 is 8.
Their absolute difference is 1 which is minimum.
We can have multiple correct answers. (1, 2, 5) and 
(3, 4) is another such group.

Input : 6
Output : 2
         6 4
         4
         5 3 2 1

我们总是可以将 n 个整数的总和划分为两个组,使得它们总和的绝对差为 0 或 1。因此组的总和最多相差 1。我们将 group1 的总和定义为 n 个元素总和的一半。
现在运行从 n 到 1 的循环,如果插入元素不超过 group1 总和,则将 i 插入 group1,否则将 i 插入 group2。

C++
// CPP program to divide n integers
// in two groups such that absolute
// difference of their sum is minimum
#include 
using namespace std;
 
// To print vector along size
void printVector(vector v)
{
    // Print vector size
    cout << v.size() << endl;
 
    // Print vector elements
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
 
    cout << endl;
}
 
// To divide n in two groups such that
// absolute difference of their sum is
// minimum
void findTwoGroup(int n)
{
    // Find sum of all elements upto n
    int sum = n * (n + 1) / 2;
 
    // Sum of elements of group1
    int group1Sum = sum / 2;
 
    vector group1, group2;
 
    for (int i = n; i > 0; i--) {
 
        // If sum is greater then or equal
        // to 0 include i in group 1
        // otherwise include in group2
        if (group1Sum - i >= 0) {
 
            group1.push_back(i);
 
            // Decrease sum of group1
            group1Sum -= i;
        }
        else {
            group2.push_back(i);
        }
    }
 
    // Print both the groups
    printVector(group1);
    printVector(group2);
}
 
// Driver program to test above functions
int main()
{
    int n = 5;
    findTwoGroup(n);
    return 0;
}


Java
// Java program to divide n integers
// in two groups such that absolute
// difference of their sum is minimum
import java.io.*;
import java.util.*;
 
class GFG
{
    // To print vector along size
    static void printVector(Vector v)
    {
        // Print vector size
        System.out.println(v.size());
     
        // Print vector elements
        for (int i = 0; i < v.size(); i++)
            System.out.print(v.get(i) + " ");
     
        System.out.println();
    }
     
    // To divide n in two groups such that
    // absolute difference of their sum is
    // minimum
    static void findTwoGroup(int n)
    {
        // Find sum of all elements upto n
        int sum = n * (n + 1) / 2;
     
        // Sum of elements of group1
        int group1Sum = sum / 2;
     
        Vector group1 = new Vector();
        Vector group2 = new Vector();
     
        for (int i = n; i > 0; i--) {
     
            // If sum is greater then or equal
            // to 0 include i in group1
            // otherwise include in group2
            if (group1Sum - i >= 0) {
     
                group1.add(i);
     
                // Decrease sum of group1
                group1Sum -= i;
            }
            else {
                group2.add(i);
            }
        }
     
        // Print both the groups
        printVector(group1);
        printVector(group2);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 5;
        findTwoGroup(n);
    }
}
 
// This code is contributed by Gitanjali.


Python3
# Python program to divide n integers
# in two groups such that absolute
# difference of their sum is minimum
import math
 
# To print vector along size
def printVector( v):
 
    # Print vector size
    print(len(v))
 
    # Print vector elements
    for i in range( 0, len(v)):
        print(v[i] , end =  " ")
 
    print()
 
 
# To divide n in two groups such that
# absolute difference of their sum is
# minimum
def findTwoGroup(n):
 
    # Find sum of all elements upto n
    sum = n * (n + 1) / 2
 
    # Sum of elements of group1
    group1Sum = sum / 2
 
    group1=[]
    group2=[]
    for i in range(n, 0, -1):
 
        # If sum is greater then or equal
        # to 0 include i in group 1
        # otherwise include in group2
        if (group1Sum - i >= 0) :
            group1.append(i)
 
            # Decrease sum of group1
            group1Sum -= i
         
        else :
            group2.append(i)
 
    # Print both the groups
    printVector(group1)
    printVector(group2)
 
# driver code
n = 5
findTwoGroup(n)
 
# This code is contributed by Gitanjali.


C#
// C# program to divide n integers
// in two groups such that absolute
// difference of their sum is minimum
using System;
using System.Collections;
 
class GFG
{
// To print vector along size
static void printVector(ArrayList v)
{
    // Print vector size
    Console.WriteLine(v.Count);
 
    // Print vector elements
    for (int i = 0; i < v.Count; i++)
        Console.Write(v[i] + " ");
 
    Console.WriteLine();
}
 
// To divide n in two groups
// such that absolute difference
// of their sum is minimum
static void findTwoGroup(int n)
{
    // Find sum of all elements upto n
    int sum = n * (n + 1) / 2;
 
    // Sum of elements of group1
    int group1Sum = sum / 2;
     
    ArrayList group1 = new ArrayList();
    ArrayList group2 = new ArrayList();
 
    for (int i = n; i > 0; i--)
    {
 
        // If sum is greater then
        // or equal to 0 include i
        // in group1 otherwise
        // include in group2
        if (group1Sum - i >= 0)
        {
            group1.Add(i);
 
            // Decrease sum of group1
            group1Sum -= i;
        }
        else
        {
            group2.Add(i);
        }
    }
 
    // Print both the groups
    printVector(group1);
    printVector(group2);
}
 
// Driver code
public static void Main()
{
    int n = 5;
    findTwoGroup(n);
}
}
 
// This code is contributed by mits


PHP
 0; $i--)
    {
 
        // If sum is greater then
        // or equal to 0 include
        // i in group 1 otherwise
        // include in group2
        if ($group1Sum - $i >= 0)
        {
 
            $group1[$x++] = $i;
 
            // Decrease sum
            // of group1
            $group1Sum -= $i;
        }
        else
        {
            $group2[$y++] = $i;
        }
    }
 
    // Print both the groups
    printVector($group1);
    printVector($group2);
}
 
// Driver Code
$n = 5;
findTwoGroup($n);
 
// This code is contributed by mits.
?>


Javascript


输出:

2
5 2
3
4 3 1

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程