Calcular la multa total a cobrar

Dada una fecha y una array de números enteros que contienen los números de los automóviles que viajan en esa fecha (un número entero), la tarea es calcular la multa total recaudada según las siguientes reglas: 
 

  • Los autos impares pueden viajar solo en fechas impares.
  • Autos pares en fechas pares solamente.
  • De lo contrario, un automóvil sería multado con 250 Rs.

Ejemplos: 
 

Input: car_num[] = {3, 4, 1, 2}, date = 15
Output: 500
Car with numbers '4' and '2' will be fined
250 each.

Input: car_num[] = {1, 2, 3} , date = 16
Output: 500
Car with numbers '1' and '3' will be fined
250 each.

Acercarse: 
 

  1. Comience a atravesar la array dada.
  2. Compruebe si el número de coche actual y la fecha no coinciden, es decir, uno es par y el otro es impar o viceversa.
  3. En caso de no coincidir cargará la multa sobre ese número de auto. De lo contrario, no.
  4. Imprime el total de la multa.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation to calculate
// the total fine collected
#include <bits/stdc++.h>
using namespace std;
 
// function to calculate the total fine collected
int totFine(int car_num[], int n, int date, int fine)
{
    int tot_fine = 0;
 
    // traverse the array elements
    for (int i = 0; i < n; i++)
 
        // if both car no and date are odd or
        // both are even, then statement
        // evaluates to true
        if (((car_num[i] ^ date) & 1) == 1)
            tot_fine += fine;
 
    // required total fine
    return tot_fine;
}
 
// Driver program to test above
int main()
{
    int car_num[] = { 3, 4, 1, 2 };
    int n = sizeof(car_num) / sizeof(car_num[0]);
    int date = 15, fine = 250;
 
    cout << totFine(car_num, n, date, fine);
 
    return 0;
}

Java

// Java implementation to calculate
// the total fine collected
class GFG
{
 
// function to calculate
// the total fine collected
static int totFine(int car_num[], int n,
                   int date, int fine)
{
int tot_fine = 0;
 
// traverse the array elements
for (int i = 0; i < n; i++)
 
    // if both car no and date
    // are odd or both are even,
    // then statement evaluates to true
    if (((car_num[i] ^ date) & 1) == 1)
        tot_fine += fine;
 
// required total fine
return tot_fine;
}
 
// Driver Code
public static void main(String[] args)
{
    int car_num[] = { 3, 4, 1, 2 };
    int n = car_num.length;
    int date = 15, fine = 250;
 
    System.out.println(totFine(car_num, n,
                               date, fine));
}
}
 
// This code is contributed
// by ChitraNayal

Python 3

# Python 3 program to calculate
# the total fine collected
 
# function to calculate the total fine collected
def totFine(car_num, n, date, fine) :
 
    tot_fine = 0
 
    # traverse the array elements
    for i in range(n) :
         
        # if both car no and date are odd or
        # both are even, then statement
        # evaluates to true
        if (((car_num[i] ^ date) & 1) == 1 ):
            tot_fine += fine
 
    # required total fine
    return tot_fine
 
# Driver Program
if __name__ == "__main__" :
 
    car_num = [ 3, 4, 1, 2 ]
    n = len(car_num)
    date, fine = 15, 250
 
    # function calling
    print(totFine(car_num, n, date, fine))
             
# This code is contributed by ANKITRAI1

C#

// C# implementation to calculate
// the total fine collected
using System;
 
class GFG
{
 
// function to calculate the
// total fine collected
static int totFine(int[] car_num, int n,
                   int date, int fine)
{
int tot_fine = 0;
 
// traverse the array elements
for (int i = 0; i < n; i++)
 
    // if both car no and date
    // are odd or both are even,
    // then statement evaluates to true
    if (((car_num[i] ^ date) & 1) == 1)
        tot_fine += fine;
 
// required total fine
return tot_fine;
}
 
// Driver Code
public static void Main()
{
    int[] car_num = { 3, 4, 1, 2 };
    int n = car_num.Length;
    int date = 15, fine = 250;
 
    Console.Write(totFine(car_num, n,
                          date, fine));
}
}
 
// This code is contributed
// by ChitraNayal

PHP

<?php
// PHP implementation to calculate
// the total fine collected
 
// function to calculate the
// total fine collected
function totFine(&$car_num, $n,
                  $date, $fine)
{
    $tot_fine = 0;
 
    // traverse the array elements
    for ($i = 0; $i < $n; $i++)
 
        // if both car no and date
        // are odd or both are even,
        // then statement evaluates
        // to true
        if ((($car_num[$i] ^
              $date) & 1) == 1)
            $tot_fine += $fine;
 
    // required total fine
    return $tot_fine;
}
 
// Driver Code
$car_num = array(3, 4, 1, 2 );
$n = sizeof($car_num);
$date = 15;
$fine = 250;
 
echo totFine($car_num, $n,
             $date, $fine);
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
 
// Javascript implementation to calculate
// the total fine collected
 
// function to calculate the total fine collected
function totFine(car_num, n, date, fine)
{
    let tot_fine = 0;
 
    // traverse the array elements
    for (let i = 0; i < n; i++)
 
        // if both car no and date are odd or
        // both are even, then statement
        // evaluates to true
        if (((car_num[i] ^ date) & 1) == 1)
            tot_fine += fine;
 
    // required total fine
    return tot_fine;
}
 
// Driver program to test above
 
    let car_num = [ 3, 4, 1, 2 ];
    let n = car_num.length;
    let date = 15, fine = 250;
 
    document.write(totFine(car_num, n, date, fine));
 
//This code is contributed by Mayank Tyagi
</script>
Producción: 

500

 

Fuente: https://www.geeksforgeeks.org/microsoft-interview-experience-for-internship/ 
 

Publicación traducida automáticamente

Artículo escrito por ayushjauhari14 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *