📜  查找在时间t站在体育场的观众人数

📅  最后修改于: 2021-04-27 19:02:13             🧑  作者: Mango

体育场内有n位观众,标记为1到n。
在时间1,第一个观众站起来。
在时间2,第二位观众站起来。

在时间k,第k个观众站立。
在时间k + 1,第(k + 1)名观众站起来,第一个观众就座。
在时间k + 2,第(k + 2)个观众站立,第二个观众坐下。

在时间n,第n个观众站立,第(n – k)个观众坐下。
在时间n +1,第(n +1-k)位观众就座。

在时间n + k,第n个观众坐下。

查找在时间t站在体育场的观众人数。

例子 :

Input : 10 5 3
Output : 3
Explanation : 
n = 10, k = 5, t = 3
At time 1, 1st spectator stands.
At time 2, 2nd spectator stands.
At time 3, 3rd spectator stands.

Thus, the result is 3 as there are 
total of 3 spectators standing.

Input :10 5 7
Output : 5
Explanation :
n = 10, k = 5, t = 7
At time 1, 1st spectator stands.
At time 2, 2nd spectator stands.
At time 3, 3rd spectator stands.
At time 4, 4th spectator stands.
At time 5, 5th spectator stands.
At time 6, 6th spectator stands
and 1st spectator sits [(n-k) = 6 - 5 = 1
as mentioned above].
At time 7, 7th spectator stands
and 2nd spectator sits.

So, now there are total of 5 spectators
standing.
Thus, result is 5.

方法 :
现在我们可以观察到此问题中的某些条件
注意:最多只能有k名观众站在体育场内。
1)如果时间小于k值,则意味着观众仍在站立。因此结果将是t。
2)在n和k(含)之间的时间中,观众站立数为k。 [观察]
3)在n次之后,观众开始一一坐下。因此,我们以“ t – n”来计算观众。然后,将k减去该值。这产生了结果。

下面是上述方法的实现:

C++
// CPP program to find number of spectators 
// standing at a time
#include 
using namespace std;
  
void result(long long n, long long k, long long t)
{
    // If the time is less than k
    // then we can print directly t time.
    if (t <= k)
        cout << t;
  
    // If the time is n then k spectators
    // are standing.
    else if (t <= n)
        cout << k;
  
    // Otherwise we calculate the spectators
    // standing.
    else {
        long long temp = t - n;
        temp = k - temp;
        cout << temp;
    }
}
  
// Driver code
int main()
{
    // Stores the value of n, k and t
    // t is time
    // n & k is the number of specators
    long long n, k, t;
    n = 10;
    k = 5;
    t = 12;
    result(n, k, t);
    return 0;
}


Java
// Java program to find number of spectators 
// standing at a time
  
class GFG { 
  
    static void result(long n, long k,long t)
    {
        // If the time is less than k
        // then we can print directly t time.
        if (t <= k)
            System.out.println(t);
       
        // If the time is n then k spectators
        // are standing.
        else if (t <= n)
            System.out.println(k);
       
        // Otherwise we calculate the 
        // spectators standing.
        else {
            long temp = t - n;
            temp = k - temp;
            System.out.println(temp);
        }
    }
       
    // Driver code
    public static void main(String args[])
    {
        // Stores the value of n, k and t
        // t is time
        // n & k is the number of specators
        long n, k, t;
        n = 10;
        k = 5;
        t = 12;
        result(n, k, t);
          
    }
}
  
/*This code is contributed by Nikita Tiwari.*/


Python3
# Python program to find number of spectators 
# standing at a time
  
def result(n, k, t) :
      
    # If the time is less than k
    # then we can print directly t time.
    if (t <= k) :
        print(t )
   
    # If the time is n then k spectators
    # are standing.
    elif (t <= n) :
        print( k)
   
    # Otherwise we calculate the 
    # spectators standing.
    else :
          
        temp = t - n
        temp = k - temp
        print (temp)
          
# Driver code
  
# Stores the value of n, k and t
# t is time
# n & k is the number of specators
n = 10
k = 5
t = 12
result(n, k, t)
  
  
# This code is contributed by Nikita Tiwari.


C#
// C# program to find number of 
// spectators standing at a time
using System;
  
class GFG { 
   
    static void result(long n, long k,long t)
    {
        // If the time is less than k
        // then we can print directly t time.
        if (t <= k)
            Console.WriteLine(t);
        
        // If the time is n then k spectators
        // are standing.
        else if (t <= n)
            Console.WriteLine(k);
        
        // Otherwise we calculate the 
        // spectators standing.
        else {
            long temp = t - n;
            temp = k - temp;
            Console.WriteLine(temp);
        }
    }
        
    // Driver code
    public static void Main()
    {
        // Stores the value of n, k and t
        // t is time
        // n & k is the number of specators
        long n, k, t;
        n = 10;
        k = 5;
        t = 12;
        result(n, k, t);
           
    }
}
   
//This code is contributed by Anant Agarwal.


PHP


输出 :

3

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