📜  一个人完成工作时仍在执行工作的人数总和

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

一个人完成工作时仍在执行工作的人数总和

给定两个表示人数的整数P和一个正整数K 。每个人都被分配了相同的工作,这恰好需要K小时才能完成。给定一个条件,即所有人都可以在X小时的时间间隔内准确地开始执行他们的工作。计算一个人完成他/她的工作时仍在执行工作的人数。任务是找到这些计数的总和

例子:

方法:给定的问题可以通过用数学的概念分析问题来解决。请按照以下步骤解决问题:

  • 找出不包括第一个(因为 P 1总是从 0 开始)和K/X总人数最小值,将其存储在变量a中。
  • 检查a是否等于0
    • 如果是,则返回 0。
    • 否则,通过数学公式计算计数的总和。
  • 返回计数的最终总和作为所需答案。

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
using namespace std;
 
// Function to find the sum of count
// of persons that are still
// performing the job
int countPersons(int P, int K, int X)
{
    // Find the minimum of total persons
    // excluding the first one and K/X
    int a = min(P - 1, K / X);
 
    // If a is equal to 0, return 0
    if (a == 0) {
        return 0;
    }
 
    // Else calculate the total sum of
    // count by the making a formula
    int ans = max(0,
                  a * (a - 1) / 2)
              + a * (P - a);
 
    // Return the sum of count
    return ans;
}
 
// Driver Code
int main()
{
    int P = 22, K = 9, X = 1;
 
    cout << countPersons(P, K, X);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG {
 
// Function to find the sum of count
// of persons that are still
// performing the job
static int countPersons(int P, int K, int X)
{
    // Find the minimum of total persons
    // excluding the first one and K/X
    int a = Math.min(P - 1, K / X);
 
    // If a is equal to 0, return 0
    if (a == 0) {
        return 0;
    }
 
    // Else calculate the total sum of
    // count by the making a formula
    int ans = Math.max(0,
                  a * (a - 1) / 2)
              + a * (P - a);
 
    // Return the sum of count
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
    int P = 22, K = 9, X = 1;
    System.out.println(countPersons(P, K, X));
}
}
// This code is contributed by Samim Hossain Mondal.


Python3
# Python program for the above approach
 
# Function to find the sum of count
# of persons that are still
# performing the job
def countPersons(P, K, X):
 
    # Find the minimum of total persons
    # excluding the first one and K/X
    a = min(P - 1, K / X)
 
    # If a is equal to 0, return 0
    if a == 0:
        return 0
 
    # Else calculate the total sum of
    # count by the making a formula
    ans = max(0,
              a * (a - 1) / 2)
 
    # Return the sum of count
    return ans + a * (P - a)
 
# Driver Code
if __name__ == "__main__":
    P = 22
    K = 9
    X = 1
    print(int(countPersons(P, K, X)))
 
# This code is contributed by Potta Lokesh


C#
// C# program for the above approach
using System;
class GFG {
 
// Function to find the sum of count
// of persons that are still
// performing the job
static int countPersons(int P, int K, int X)
{
    // Find the minimum of total persons
    // excluding the first one and K/X
    int a = Math.Min(P - 1, K / X);
 
    // If a is equal to 0, return 0
    if (a == 0) {
        return 0;
    }
 
    // Else calculate the total sum of
    // count by the making a formula
    int ans = Math.Max(0,
                  a * (a - 1) / 2)
              + a * (P - a);
 
    // Return the sum of count
    return ans;
}
 
// Driver Code
public static void Main()
{
    int P = 22, K = 9, X = 1;
    Console.Write(countPersons(P, K, X));
}
}
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
153

时间复杂度: O(1)
辅助空间: O(1)