📜  查找击球手的击球平均值

📅  最后修改于: 2021-04-23 18:32:59             🧑  作者: Mango

给定三个整数runsmatchsnot-out分别代表计分的奔跑次数,击球手打的局数和保持击球的次数,任务是计算击球手的击球平均值。

注意:如果击球手从未被解雇,请打印“ NA”,因为可以定义无平均值。

例子:

方法:
请按照以下步骤解决问题:

  1. 计算被解雇的次数,等于比赛-notout
  2. 计算平均击球次数,等于运行次数/(比赛次数-不在)

下面是上述方法的实现:

C++
// C++ program to calculate
// the average of a batsman
#include 
using namespace std;
 
// Function to find the average
// of a batsman
double averageRuns(int runs,
                   int matches,
                   int notout)
{
    // Calculate number of
    // dismissals
    int out = matches - notout;
 
    // check for 0 times out
    if (out == 0)
        return -1;
 
    // Calculate batting average
    double avg = double(runs) / out;
 
    return avg;
}
 
// Driver Program
int main()
{
    int runs = 10000;
    int matches = 250;
    int notout = 50;
 
    double avg
        = averageRuns(
            runs, matches, notout);
 
    if (avg == -1)
        cout << "NA";
    else
        cout << avg;
 
    return 0;
}


Java
// Java program to calculate
// the average of a batsman
class GFG{
     
// Function to find the average
// of a batsman
static int averageRuns(int runs,
                       int matches,
                       int notout)
{
     
    // Calculate number of
    // dismissals
    int out = matches - notout;
 
    // Check for 0 times out
    if (out == 0)
        return -1;
 
    // Calculate batting average
    int avg = (runs) / out;
 
    return avg;
}
 
// Driver code
public static void main(String[] args)
{
    int runs = 10000;
    int matches = 250;
    int notout = 50;
 
    int avg = averageRuns(runs, matches,
                                notout);
    if (avg == -1)
        System.out.print("NA");
    else
        System.out.print(avg);
}
}
 
// This code is contributed by Ritik Bansal


Python3
# Python3 program to calculate
# the average of a batsman
 
# Function to find the average
# of a batsman
def averageRuns(runs, matches, notout):
 
    # Calculate number of
    # dismissals
    out = matches - notout;
 
    # check for 0 times out
    if (out == 0):
        return -1;
 
    # Calculate batting average
    avg = runs // out;
 
    return avg;
 
# Driver Program
runs = 10000;
matches = 250;
notout = 50;
 
avg = averageRuns(runs, matches, notout);
 
if (avg == -1):
    print("NA");
else:
    print(avg);
 
# This code is contributed by Akanksha_rai


C#
// C# program to calculate
// the average of a batsman
using System;
class GFG{
      
// Function to find the average
// of a batsman
static int averageRuns(int runs,
                       int matches,
                       int notout)
{
      
    // Calculate number of
    // dismissals
    int out1;
    out1 = matches - notout;
  
    // Check for 0 times out
    if (out1 == 0)
        return -1;
  
    // Calculate batting average
    int avg = (runs) / out1;
  
    return avg;
}
  
// Driver code
public static void Main (string[] args)
{
    int runs = 10000;
    int matches = 250;
    int notout = 50;
  
    int avg = averageRuns(runs, matches,
                                notout);
    if (avg == -1)
        Console.Write("NA");
    else
        Console.Write(avg);
}
}
  
// This code is contributed by rock_cool


Javascript


输出:
50

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