📌  相关文章
📜  将数字分成 N 个部分,使得最小和最大部分之间的差异最小

📅  最后修改于: 2021-10-26 06:54:00             🧑  作者: Mango

给定两个整数“X”和“N”,任务是将整数“X”拆分为“N”个部分,使得:
X1 + X2 + X3 + … + Xn = X 并且最小化序列中最大和最小数之间的差异。
最后打印序列,如果数字不能被精确地分成 ‘N’ 个部分,则打印 ‘-1’ 代替。
例子:

方法:如果 X >= N,总有一种拆分数字的方法。

  • 如果数字被精确地拆分为“N”个部分,那么每个部分都将具有值 X/N,其余的 X%N 部分可以分布在任何 X%N 个数字中。
  • 因此,如果 X % N == 0 那么最小差异将始终为 ‘0’ 并且该序列将包含所有相等的数字,即 x/n。
  • 否则,差异将是“1”,序列将是 X/N、X/N、…、(X/N)+1、(X/N)+1..

下面是上述方法的实现:

C++
// CPP implementation of the approach
#include
using namespace std;;
 
// Function that prints
// the required sequence
void split(int x, int n)
{
 
// If we cannot split the
// number into exactly 'N' parts
if(x < n)
cout<<"-1"<<" ";
 
         
 
    // If x % n == 0 then the minimum
    // difference is 0 and all
    // numbers are x / n
    else if (x % n == 0)
    {
        for(int i=0;i= zp)
            cout<<(pp + 1)<<" ";
            else
            cout<


Java
// Java implementation of the approach
  
class GFG{
// Function that prints
// the required sequence
static void split(int x, int n)
{
  
// If we cannot split the
// number into exactly 'N' parts
if(x < n)
System.out.print("-1 ");
  
          
  
    // If x % n == 0 then the minimum
    // difference is 0 and all
    // numbers are x / n
    else if (x % n == 0)
    {
        for(int i=0;i= zp)
            System.out.print((pp + 1)+" ");
            else
            System.out.print(pp+" ");
        }
    }
}
      
// Driver code
public static void main(String[] args)
{
          
int x = 5;
int n = 3;
split(x, n);
  
}
}
//This code is contributed by mits


Python3
# Python3 implementation of the approach
 
# Function that prints
# the required sequence
def split(x, n):
 
    # If we cannot split the
    # number into exactly 'N' parts
    if(x < n):
        print(-1)
 
    # If x % n == 0 then the minimum
    # difference is 0 and all
    # numbers are x / n
    elif (x % n == 0):
        for i in range(n):
            print(x//n, end =" ")
    else:
        # upto n-(x % n) the values
        # will be x / n
        # after that the values
        # will be x / n + 1
        zp = n - (x % n)
        pp = x//n
        for i in range(n):
            if(i>= zp):
                print(pp + 1, end =" ")
            else:
                print(pp, end =" ")
       
# Driver code         
x = 5
n = 3
split(x, n)


C#
// C# implementation of the approach
using System;
 
public class GFG{
    // Function that prints
// the required sequence
static void split(int x, int n)
{
 
// If we cannot split the
// number into exactly 'N' parts
if(x < n)
Console.WriteLine("-1 ");
 
         
 
    // If x % n == 0 then the minimum
    // difference is 0 and all
    // numbers are x / n
    else if (x % n == 0)
    {
        for(int i=0;i= zp)
            Console.Write((pp + 1)+" ");
            else
            Console.Write(pp+" ");
        }
    }
}
     
// Driver code
static public void Main (){
 
int x = 5;
int n = 3;
split(x, n);
 
}
}
//This code is contributed by Sachin.


PHP
= $zp)
            {
                echo (int)$pp + 1;
                echo (" ");
            }
            else
            {
                echo (int)$pp;
                echo (" ");
            }
        }
    }
}
 
// Driver code    
$x = 5;
$n = 3;
split( $x, $n);
 
// This code is contributed
// by Shivi_Aggarwal
?>


Javascript


输出:
1 2 2

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