📜  总和为 N 的 M 个非负整数的随机列表

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

给定两个整数MN ,任务是创建一个由M 个非负整数组成的列表,其总和为N 。如果可能有多个列表,请找到任何一个。
例子:

方法:要获得一个完整的随机整数列表,创建一个大小为M的数组,其中每个元素都用0初始化。现在运行一个从0N – 1的循环,并使用 rand()函数将数组中任何随机选择的元素增加1 。这样,结果列表的总和将为N
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Utility function to print the
// elements of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to generate a list of
// m random non-negative integers
// whose sum is n
void randomList(int m, int n)
{
 
    // Create an array of size m where
    // every element is initialized to 0
    int arr[m] = { 0 };
    srand(time(0));
 
    // To make the sum of the final list as n
    for (int i = 0; i < n; i++) {
 
        // Increment any random element
        // from the array by 1
        arr[rand() % m]++;
    }
 
    // Print the generated list
    printArr(arr, m);
}
 
// Driver code
int main()
{
    int m = 4, n = 8;
 
    randomList(m, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Utility function to print the
// elements of an array
static void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
}
 
// Function to generate a list of
// m random non-negative integers
// whose sum is n
static void randomList(int m, int n)
{
 
    // Create an array of size m where
    // every element is initialized to 0
    int arr[] = new int[m];
     
    // To make the sum of the final list as n
    for (int i = 0; i < n; i++)
    {
 
        // Increment any random element
        // from the array by 1
        arr[(int)(Math.random() * m)]++;
    }
 
    // Print the generated list
    printArr(arr, m);
}
 
// Driver code
public static void main(String args[])
{
    int m = 4, n = 8;
 
    randomList(m, n);
}
}
 
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the approach
from random import randint
 
# Utility function to print the
# elements of an array
def printArr(arr, n) :
 
    for i in range(n) :
        print(arr[i], end = " ");
 
# Function to generate a list of
# m random non-negative integers
# whose sum is n
def randomList(m, n):
 
    # Create an array of size m where
    # every element is initialized to 0
    arr = [0] * m;
     
    # To make the sum of the final list as n
    for i in range(n) :
 
        # Increment any random element
        # from the array by 1
        arr[randint(0, n) % m] += 1;
 
    # Print the generated list
    printArr(arr, m);
 
# Driver code
if __name__ == "__main__" :
 
    m = 4; n = 8;
 
    randomList(m, n);
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Utility function to print the
// elements of an array
static void printArr(int []arr, int n)
{
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
 
// Function to generate a list of
// m random non-negative integers
// whose sum is n
static void randomList(int m, int n)
{
 
    // Create an array of size m where
    // every element is initialized to 0
    int [] arr = new int[m];
     
    // To make the sum of the final list as n
    for (int i = 0; i < n; i++)
    {
 
        // Increment any random element
        // from the array by 1
        Random rnd = new Random();
 
        arr[rnd.Next(0, n) % m]++;
    }
 
    // Print the generated list
    printArr(arr, m);
}
 
// Driver code
public static void Main()
{
    int m = 4, n = 8;
 
    randomList(m, n);
}
}
 
// This code is contributed by Mohit kumar


Javascript


输出:

1 3 3 1

时间复杂度: O(max(M, N))

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