📌  相关文章
📜  N掷硬币后正面和反面的总数

📅  最后修改于: 2021-04-21 23:54:50             🧑  作者: Mango

给定字符C和一个整数N,表示C位置的N个硬币,其中C可以是。我们可以翻转硬币N次,其中第i轮玩家将翻转所有小于或等于i的硬币的面孔。任务是确定翻转N次可能的时间后头和尾的总数。

例子:

方法:
为了解决上述问题,我们必须遵循以下步骤:

  • 在上面的问题中,如果我们观察到,则存在一种模式,如果最初所有硬币都朝向方向,则N轮后的头总数将为(n / 2)的底值,而尾数将为单元格(n / 2)的值。
  • 否则,如果所有硬币都朝向尾巴方向,则N轮后的尾巴总数将为底值(n / 2),而头将为ceil值(n / 2)。

下面是实现:

C++
// C++ program to count total heads
// and tails after N flips in a coin
#include
using namespace std;
 
// Function to find count of head and tail
pair count_ht(char s, int N)
{
     
    // Check if initially all the
    // coins are facing towards head
    pairp;
    if(s == 'H')
    {
        p.first = floor(N / 2.0);
        p.second = ceil(N / 2.0);
    }
     
    // Check if initially all the coins
    // are facing towards tail
    else if(s == 'T')
    {
        p.first = ceil(N / 2.0);
        p.second = floor(N / 2.0);
    }
     
    return p;
}
 
// Driver code
int main()
{
    char C = 'H';
    int N = 5;
     
    pair p = count_ht(C, N);
     
    cout << "Head = " << (p.first) << "\n";
    cout << "Tail = " << (p.second) << "\n";
}
 
// This code is contributed by virusbuddah_


Java
// Java program to count
// total heads and tails
// after N flips in a coin
import javafx.util.Pair;
public class Main
{
// Function to find count of head and tail 
public static Pair  count_ht(char s,
                                      int N)
{              
  // Check if initially all the 
  // coins are facing towards head 
  Pair  p = new Pair  (0, 0);
  if(s == 'H')
  {
     p = new Pair  ((int)Math.floor(N / 2.0),
                             (int)Math.ceil(N / 2.0));
  }
 
  // Check if initially all the coins 
  // are facing towards tail 
  else if(s == 'T')
  {
     p = new Pair  ((int)Math.ceil(N / 2.0),
                             (int)Math.floor(N / 2.0));
  }
  return p;
}
 
public static void main(String[] args)
{
  char C = 'H';
  int N = 5;
  Pair  p = count_ht(C, N);
  System.out.println("Head = " + p.getKey());
  System.out.println("Tail = " + p.getValue());
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to Count total heads
# and tails after N flips in a coin
 
# Function to find count of head and tail
import math
def count_ht( s, N ):
     
    # Check if initially all the
    # coins are facing towards head
    if s == "H":
        h = math.floor( N / 2 )
        t = math.ceil( N / 2 )
         
    # Check if initially all the coins
    # are facing towards tail
    elif s == "T":
        h = math.ceil( N / 2 )
        t = math.floor( N / 2 )
         
    return [h, t]
 
# Driver Code
if __name__ == "__main__":
    C = "H"
    N = 5
    l = count_ht(C, n)
    print("Head = ", l[0])
    print("Tail = ", l[1])


C#
// C# program to count total heads
// and tails after N flips in a coin
using System;
 
class GFG{
     
// Function to find count of head and tail 
public static Tuple count_ht(char s,
                                       int N)
{ 
     
    // Check if initially all the 
    // coins are facing towards head 
    Tuple p = Tuple.Create(0, 0);
     
    if (s == 'H')
    {
        p = Tuple.Create((int)Math.Floor(N / 2.0),
                         (int)Math.Ceiling(N / 2.0));
    }
     
    // Check if initially all the coins 
    // are facing towards tail 
    else if (s == 'T')
    {
        p = Tuple.Create((int)Math.Ceiling(N / 2.0),
                         (int)Math.Floor(N / 2.0));
    }
    return p;
}
 
// Driver Code
static void Main()
{
    char C = 'H';
    int N = 5;
    Tuple p = count_ht(C, N);
     
    Console.WriteLine("Head = " + p.Item1);
    Console.WriteLine("Tail = " + p.Item2);
}
}
 
// This code is contributed by divyesh072019


输出:
Head =  2
Tail =  3

时间复杂度: O(1)