Friday 27 July 2018

Peter and Casino
 
Hotel Royal Gardenia has arranged for an elite business party for the lead industrialists and celebrities of the City. Followed by a dinner buffet, the Event coordinators planned for some casino game events for the high-toned crowd. Peter was a visitor at the party and he takes some number of rubles to the casino with the intention of becoming rich. He plays three machines in turn. Unknown to him, the machines are entirely predictable. Each play costs one ruble. The first machine pays 20 rubles every 25th time it is played; the second machine pays 80 rubles every 120th time it is played; the third pays 8 rubles every 12th time it is played.
Given the number of rubles with Peter initially (there will be at least one and fewer than 1000), and the number of times each machine has been played since it last paid, write a program that calculates the number of times Peter plays until he goes broke.


Input Format:
First line of the input is an integer that corresponds to the number of rubles with Peter initially.
Next 3 lines of the input is an integer that corresponds to the number of times each machine has been played since it last paid.

Output Format:
Output a single line that gives the number of times Peter plays until he goes broke.
Refer sample input and output for formatting specifications.
Sample Input 1:
48
3
12
4

Sample Output 1:
Peter plays 56 times before going broke

Solution:
#include<stdio.h>
int main()
{
    int r,a,b,c,count=0;
    scanf("%d%d%d%d",&r,&a,&b,&c);
    while(r>0)
    {
        if(a==25)
        {
            r=r+20;
        }
        r--;
        count++;
        if(r==0)
        break;
       
        if(b==120)
        {
            r=r+80;
        }
        r--;
        count++;
        if(r==0)
        break;
        
        if(c==12)
        {
            r=r+8;
        }
        r--;
        count++;
        if(r==0)
        break;
        a++,b++,c++;
        
    }
    
    printf("Peter plays %d times before going broke",count);
    return 0;
    
    
}

24 comments:

  1. But the following input is not working
    35
    10
    30
    9
    Answer 71 but this code is not satisfy this test case.

    ReplyDelete
    Replies
    1. just put increment term above if condition

      Delete
    2. I can't understand this

      Delete
    3. bro i placed a++,b++,c++ below the while but still notworking for other test case please help,THank you

      Delete
    4. same doubt for me also
      have u got that solution

      Delete
    5. This comment has been removed by the author.

      Delete
    6. #include
      int main(){
      int r,m1,m2,m3;
      scanf("%d %d %d %d",&r,&m1,&m2,&m3);
      int c1=0,c2=0,c3=0;
      int n1=0,n2=0,n3=0;
      int t1=25-m1;
      int t2=120-m2;
      int t3=12-m3;
      while(r>0){
      c1++;
      if(n1==0 && c1==t1){
      r+=20;
      r--;
      n1=t1+25;
      }
      else if(c1==n1){
      n1+=25;
      r+=20;
      r--;
      }
      else{
      r--;
      if(r==0)
      break;
      }
      c2++;
      if(n2==0 && c2==t2){
      r+=80;
      r--;
      n2=t2+120;
      }
      else if(c2==n2){
      n2+=120;
      r+=80;
      r--;
      }
      else{
      r--;
      if(r==0)
      break;
      }
      c3++;
      if(c3==t3 && n3==0){
      r+=8;
      r--;
      n3=t3+12;

      }
      else if(c3==n3){
      n3+=12;
      r+=8;
      r--;
      }
      else{
      r--;
      if(r==0)
      break;
      }
      }
      printf("Peter plays %d times before broke",c1+c2+c3);

      return 0;
      }

      Delete
    7. Use % function in the if condition
      Ex - if(a%25==0)
      Since it gives 20 every 25th time

      Delete
    8. #include
      int main(){
      int r,m1,m2,m3; //input variables
      scanf("%d %d %d %d",&r,&m1,&m2,&m3);
      int c1=0,c2=0,c3=0; //counter for each machine

      //number of pays to refund - only required for 0th case
      int t1=25-m1; //rolls left after the last state of machine to reach refund
      int t2=120-m2;
      int t3=12-m3;

      int n1=0,n2=0,n3=0; //after 0th case n1 takes over

      while(r>0){
      c1++; //m1 transaction
      if(n1==0/*0th case*/ && c1==t1 /*rolls reached*/){
      r+=20; // refund
      r--; //for that round
      n1=t1+25; /*n1 takes over t1 -> 25 is added to t1 and saved in n1 - this will be the target when
      the machine will again give refund*/
      }
      else if(c1==n1)/* next target reached when refund will be given*/{
      n1+=25; // target incremented
      r+=20; //refund
      r--; // for the round
      }
      else{ //other cases when he just loses the rubles
      r--;
      if(r==0) // if the rubles are depleated he goes broke and he cant play any further
      break;
      }

      c2++;
      if(n2==0 && c2==t2){
      r+=80;
      r--;
      n2=t2+120;
      }
      else if(c2==n2){
      n2+=120;
      r+=80;
      r--;
      }
      else{
      r--;
      if(r==0)
      break;
      }

      c3++;
      if(c3==t3 && n3==0){
      r+=8;
      r--;
      n3=t3+12;
      }
      else if(c3==n3){
      n3+=12;
      r+=8;
      r--;
      }
      else{
      r--;
      if(r==0)
      break;
      }
      }
      printf("Peter plays %d times before broke",c1+c2+c3);
      return 0;
      }

      /* Just Documented it so that the code becomes clear for everyone */

      Delete
    9. does it work for input 35, 10,30,9

      Delete
  2. Wonderful illustrated information. I thank you about that. No doubt it will be very useful for my future projects. Would like to see some other posts on the same subject!
    MKT Vegas831

    ReplyDelete
  3. Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative. i would like to bookmark the page so i can come here again to read you. as you have done a wonderful job.
    XE88 Download FataBet

    ReplyDelete
  4. I am looking for and I love to post a comment that "The content of your post is awesome" Great work! promo judi slot

    ReplyDelete
  5. Perhaps you have played lots of casino online games. If you don't know famous Indian online casino games or never played these games then you can read about these games here. These games are so interesting and enjoyable to play.

    ReplyDelete
  6. Excellent information on your blog, thank you for taking the time to share with us. Amazing insight you have on this, it's nice to find a website that details so much information about different artists.
    login joker123

    ReplyDelete
  7. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..
    dominoqq online

    ReplyDelete
  8. can u explain me the above code

    ReplyDelete
  9. Thank you so much for sharing

    ReplyDelete
  10. I really appreciate your support on this.
    Look forward to hearing from you soon.
    I’m happy to answer your questions, if you have any.


    คาสิโน

    เล่นบาคาร่า

    เครดิตฟรี

    แจกเครดิตฟรี ฝากถอนง่าย

    ReplyDelete
  11. Many thanks for your kind invitation. I’ll join you.
    Would you like to play cards?
    Come to the party with me, please.
    See you soon...

    เครดิตฟรี

    คาสิโนออนไลน์

    เล่นบาคาร่า

    คาสิโนออนไลน์

    ReplyDelete
  12. #include
    int main(){
    int r,m1,m2,m3; //input variables
    scanf("%d %d %d %d",&r,&m1,&m2,&m3);
    int c1=0,c2=0,c3=0; //counter for each machine

    //number of pays to refund - only required for 0th case
    int t1=25-m1; //rolls left after the last state of machine to reach refund
    int t2=120-m2;
    int t3=12-m3;

    int n1=0,n2=0,n3=0; //after 0th case n1 takes over

    while(r>0){
    c1++; //m1 transaction
    if(n1==0/*0th case*/ && c1==t1 /*rolls reached*/){
    r+=20; // refund
    r--; //for that round
    n1=t1+25; /*n1 takes over t1 -> 25 is added to t1 and saved in n1 - this will be the target when
    the machine will again give refund*/
    }
    else if(c1==n1)/* next target reached when refund will be given*/{
    n1+=25; // target incremented
    r+=20; //refund
    r--; // for the round
    }
    else{ //other cases when he just loses the rubles
    r--;
    if(r==0) // if the rubles are depleated he goes broke and he cant play any further
    break;
    }

    c2++;
    if(n2==0 && c2==t2){
    r+=80;
    r--;
    n2=t2+120;
    }
    else if(c2==n2){
    n2+=120;
    r+=80;
    r--;
    }
    else{
    r--;
    if(r==0)
    break;
    }

    c3++;
    if(c3==t3 && n3==0){
    r+=8;
    r--;
    n3=t3+12;
    }
    else if(c3==n3){
    n3+=12;
    r+=8;
    r--;
    }
    else{
    r--;
    if(r==0)
    break;
    }
    }
    printf("Peter plays %d times before broke",c1+c2+c3);
    return 0;
    }

    ReplyDelete
  13. Will this logic work ?

    import java.util.Scanner;
    class HelloWorld {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Value rubbles: ");
    int rubbles = sc.nextInt();
    System.out.print("Value A: ");
    int a = sc.nextInt();
    System.out.print("Value B: ");
    int b = sc.nextInt();
    System.out.print("Value C: ");
    int c = sc.nextInt();

    int turn = 0;
    int count = 0;
    while(rubbles > 0)
    {
    if(turn % 3 == 0){
    rubbles -= 1;
    a += 1;
    if(a % 25 == 0)
    rubbles += 20;
    }
    else if(turn % 3 == 1){
    rubbles -= 1;
    b += 1;
    if(b % 120 == 0)
    rubbles += 80;
    }
    else{
    rubbles -= 1;
    c += 1;
    if(c % 12 == 0)
    rubbles += 8;
    }
    turn +=1;
    count+=1;

    }
    System.out.println(count);
    }
    }

    ReplyDelete

Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...