📜  查找参加聚会的所有客人

📅  最后修改于: 2022-05-13 01:57:25.367000             🧑  作者: Mango

查找参加聚会的所有客人

一个人举办派对并邀请 N 位客人参加。但是,每个客人都有一个条件,即每个客人 'Gi' 仅在聚会中至少有 'Pi' 人时才留在聚会上,否则离开。客人的总数 N 和每位客人需要的人数“Pi”作为每位客人的输入给出。任务是找到参加聚会的所有客人。还给出了客人按照数组“Pi”中给出的顺序到达聚会的情况
例子:

Input: N = 5, Pi = {1, 0, 2, 1, 3}
Output: 2
Explanation: 
Since 5 guests are invited to the party.
Total guest present initially = 0

For Guest number 1:
The 1st guest needs at least 1 person, 
since he is the first to arrive, 
and there is no one else, so he leaves.
Therefore, Total guest so far = 0

For Guest number 2:
The 2nd guest needs 0 people, so he stays. 
Therefore, Total guest so far = 0 + 1 = 1

For Guest number 3:
The 3rd guest needs at least 2 people,
And there are still only 1 guest present,
so he leaves.
Therefore, Total guest so far = 1 + 0 = 1

For Guest number 4:
The 4th guest needs at least 1 people,
And there is 1 guest present, so he stays. 
Therefore, Total guest so far = 1 + 1 = 2

For Guest number 5:
The 5th guest needs at least 3 people,
And there is only 2 guest present, so he leaves. 
Therefore, Total guest so far = 2 + 0 = 2

Total guests that are present at the party = 2.

Input: N = 3, Pi = {0, 2, 1}
Output: 2
Explanation: 
Since 3 guests are invited to the party.
Total guest present initially = 0

For Guest number 1:
The 1st guest needs 0 people, so he stays.
Therefore, Total guest so far = 1

For Guest number 2:
The 2nd guest needs at least 2 people,
And there are still only 1 guest present,
so he leaves.
Therefore, Total guest so far = 1 + 0 = 1

For Guest number 3:
The 3rd guest needs at least 1 people,
And there is 1 guest present, so he stays. 
Therefore, Total guest so far = 1 + 1 = 2

Total guests that are present at the party = 2.

学生将在这些免费课程中了解更多关于编程的世界,这肯定会帮助他们在未来做出明智的职业选择。

方法:

  • 获取N中邀请的客人数量和数组guest[]中每个客人的要求。
  • 将 totalGuests 初始化为 0,作为出席的客人总数。
  • 在数组guest[] 中从0 到N-1 迭代。
  • 如果客人的要求小于或等于 totalGuests,则将 totalGuests 增加 1
  • 当完整数组guest[]被遍历后,打印guests的总数'totalGuests'

执行:

C++
// C++ program to get the
// total number of guests at the party
 
#include 
using namespace std;
 
// Function to find the totalGuests
int findGuest(int array[], int N)
{
    // Total guest before the party are 0
    int count = 0;
 
    // Checking requirements for each guest
    for (int i = 0; i < N; i++) {
 
        // If requirements are met
        if (array[i] <= count) {
 
            // The Gi guest decides to stay
            // So increment total guest by 1
            count++;
        }
    }
 
    // Return the totalnumber of guest
    return count;
}
 
// Driver code
int main()
{
 
    // Get the number of guests invited
    int N = 5;
 
    // Guests array stores
    // the requirement by each guest
    int guests[] = { 1, 0, 2, 1, 3 };
 
    // Get the total number of guests present
    int totalGuests = findGuest(guests, N);
 
    cout << totalGuests << endl;
 
    return 0;
}


Java
// Java program to get the
// total number of guests at the party
class GFG
{
     
// Function to find the totalGuests
static int findGuest(int array[], int N)
{
    // Total guest before the party are 0
    int count = 0;
 
    // Checking requirements for each guest
    for (int i = 0; i < N; i++)
    {
 
        // If requirements are met
        if (array[i] <= count)
        {
 
            // The Gi guest decides to stay
            // So increment total guest by 1
            count++;
        }
    }
 
    // Return the totalnumber of guest
    return count;
}
 
// Driver code
public static void main(String[] args)
{
 
    // Get the number of guests invited
    int N = 5;
 
    // Guests array stores
    // the requirement by each guest
    int guests[] = { 1, 0, 2, 1, 3 };
 
    // Get the total number of guests present
    int totalGuests = findGuest(guests, N);
 
    System.out.println(totalGuests);
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 program to get the
# total number of guests at the party
 
# Function to find the totalGuests
def findGuest(guests, N):
    count = 0
 
    # Checking requirements for each guest
    for i in range(N):
 
        # If requirements are met
        if guests[i] <= count:
 
            # The Gi guest decides to stay
            # So increment total guest by 1
            count += 1
             
    # Return the totalnumber of gues
    return count
 
# Driver code
N = 5
guests = [1, 0, 2, 1, 3]
totalGusets = findGuest(guests, N)
print(totalGusets)
 
# This code is contributed by Shrikant13


C#
// C# program to get the
// total number of guests at the party
using System;
 
class GFG
{
         
    // Function to find the totalGuests
    static int findGuest(int [] array, int N)
    {
        // Total guest before the party are 0
        int count = 0;
     
        // Checking requirements for each guest
        for (int i = 0; i < N; i++)
        {
     
            // If requirements are met
            if (array[i] <= count)
            {
     
                // The Gi guest decides to stay
                // So increment total guest by 1
                count++;
            }
        }
     
        // Return the totalnumber of guest
        return count;
    }
     
    // Driver code
    public static void Main ()
    {
     
        // Get the number of guests invited
        int N = 5;
     
        // Guests array stores
        // the requirement by each guest
        int [] guests = { 1, 0, 2, 1, 3 };
     
        // Get the total number of guests present
        int totalGuests = findGuest(guests, N);
     
        Console.WriteLine(totalGuests);
    }
}
 
// This code is contributed by ihritik


PHP


Javascript


输出:
2