Going to the cinema

kata programming

مساله:

دوست من John دوست دارد به سینما برود. او می تواند بین سیستم A و B یکی را انتخاب کند.

سیستم A: همیشه قیمت بلیط ثابت است.

سیستم B: او اول باید کارت اشتراک با یک قیمت مشخص بگیرد. در مواقع بعد هر دفعه 0.9 دفعه ی پیش را پرداخت می کند.

برای مثال:

اگر John برای 3 بار به سینما برود:

System A : 15 * 3 = 45
System B : 500 + 15 * 0.90 + (15 * 0.90) * 0.90 + (15 * 0.90 * 0.90) * 0.90 ( = 536.5849999999999, no rounding for each ticket)

جان می خواهد بداند چند بار باید به سینما برود تا نتیجه نهایی سیستم B، وقتی رند شود به دلار، ارزان تر از سیستم A می شود.

تابع movie دارای 3 ورودی است:

  • card: قیمت حق اشتراک کارت
  • ticket: قیمت بلیط
  • perc: بخشی از مبلغی که برای بلیط قبلی پرداخته است

مقدار n که تعداد است را باید برگردانید به صورتی که:

ceil(price of System B) < price of System A.

مثال های بیشتر:

movie(500, 15, 0.9) should return 43 
    (with card the total price is 634, with tickets 645)
movie(100, 10, 0.95) should return 24 
    (with card the total price is 235, with tickets 240)

Description:

My friend John likes to go to the cinema. He can choose between system A and system B.

System A : he buys a ticket (15 dollars) every time
System B : he buys a card (500 dollars) and a first ticket for 0.90 times the ticket price, 
then for each additional ticket he pays 0.90 times the price paid for the previous ticket.

Example:

If John goes to the cinema 3 times:

System A : 15 * 3 = 45
System B : 500 + 15 * 0.90 + (15 * 0.90) * 0.90 + (15 * 0.90 * 0.90) * 0.90 ( = 536.5849999999999, no rounding for each ticket)

John wants to know how many times he must go to the cinema so that the final result of System B, when rounded up to the next dollar, will be cheaper than System A.

The function movie has 3 parameters: card (price of the card), ticket (normal price of a ticket), perc (fraction of what he paid for the previous ticket) and returns the first n such that

ceil(price of System B) < price of System A.

More examples:

movie(500, 15, 0.9) should return 43 
    (with card the total price is 634, with tickets 645)
movie(100, 10, 0.95) should return 24 
    (with card the total price is 235, with tickets 240)

public class Movie {
    
    public static int movie(int card, int ticket, double perc) {
        int count = 0;
        double totalB = card;
        
        while (ticket * count <= Math.ceil(totalB)) totalB += ticket * Math.pow(perc, ++count);

        return count;
    }
}
public class Movie {

    public static int movie(int card, int ticket, double perc) {
        double systemA = 0, systemB = card, tempTicket = ticket;
        int numberOfVisits = 0;
        do {
            systemA += ticket;
            tempTicket *= perc;
            systemB += tempTicket;
            numberOfVisits++;
        } while (systemA <= Math.ceil(systemB));

        return numberOfVisits;
    }
}
class Movie{static int movie(int r,int t,double p){float a=0,b=r,c=t;while(b+1>=a){a+=t;b+=c;c*=p;}return(int)a/t-1;}}
function movie($card, $ticket, $perc) {
    $need = 0;
    do {
        $need++;
        $card += $ticket * ($perc ** $need);
    } while (ceil($card) >= ($ticket * $need));   
    return $need;
}
function movie($card, $ticket, $perc)
{
  for ($i = 0; $ticket * $i <= ceil($card); $i++) {
    $card += $ticket * ($perc ** $i);
  }

  return $i - 1;
}
function movie($card, $ticket, $perc) {
    $ta = 0;
    $tb = $card;
    $i = 0;
    do {
        $i++;
        $ta += $ticket;
        $tb += $ticket * pow($perc, $i);
    } while (ceil($ta) <= ceil($tb));
    return $i;
}
function movie($card, $ticket, $perc) {
    $n = 0;
    $ticketB = $ticket;
    $total = $card;
    do {
        $n++;
        $ticketB *= $perc;
        $total += $ticketB;
    } while(ceil($total)>=$ticket*$n);
    return $n;
}
function movie($card, $ticket, $perc) {
    $i = 0; $sb = $card; $sa = 0; $prev = $ticket;
    while (true) {
        $i++; $nou = $prev * $perc; $sb += $nou;
        $prev = $nou; $sa += $ticket;
        if (ceil($sb) < $sa)
            return $i;
    }
}

دیدگاهتان را بنویسید