We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. Algorithms
  3. Implementation
  4. Chocolate Feast
  5. Discussions

Chocolate Feast

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial
  • Topics

    You are viewing a single comment's thread. Return to all comments →

  • sattujaiswal 4 years ago+ 4 comments

    static int cc; public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int t,n,c,m,r;
            t = in.nextInt();
            while(t-->0){
             n = in.nextInt();
            c = in.nextInt();
             m = in.nextInt();
                r=n/c;
                cc=r;
    
                    while(r>=m){
                        cc=cc+r/m;
                        r=r%m+r/m;
                    }
    
                System.out.println(cc); 
            }
    
    }
    

    }

    2|
    ParentPermalink
    • evilcat 4 years ago+ 0 comments

      I am (currently) just on hackerranks to have "exercises" to learn java/programming with and look at neat solutions and tips after getting the solution my own way and see if i can improve it.

      I loved the neatness of the above solution and thought wow that must run so much faster than my solution, so i submitted them one after another a few times only to see my solution outperform the above. Why? Is the remainder operator in the above solution really so expensive and my jumble of code faster just because it doesn't use it?

      import java.io.; import java.util.;

      public class Solution {

      public static void main(String[] args) {
      
          Scanner sc = new Scanner(System.in);
          int t = Integer.parseInt(sc.nextLine());    //t = number of test cases
          int[][] tc = readInput(sc, t);              //tc[t][0] = money. tc[t][1] = price. tc[t][2] = wrappers per free bar
      
          for (int i = 0; i<t; i++){                  //loop for all test cases
              int choc = calcChoc(tc,i);              //work out how much choc can be bought
              System.out.println(choc);               //print result for the test case
          }
      }
      //calculate how much choc he can buy with m $ at p price with w wrappers needed for a free bar
      public static int calcChoc(int[][] tc,int i){
      
          int m = tc[i][0];       //money he has
          int p = tc[i][1];       //price of choc
          int w = tc[i][2];       //wrappers per free bar
      
          int bars = m/p;         //how many bars he can buy initially
          int wrappers = bars;    //each bar is a wrapper from initial purpose
      
          //loop to turn in all wrappers while it is possible to do so
          while (w<=wrappers){
      
              int barsFromTurnIn = wrappers/w;                //bars from turning in current wrappers.
              bars = bars + barsFromTurnIn;                   //new bar count
              wrappers = wrappers - (barsFromTurnIn * (w-1)); //wrapper count reduced by amount of wrappers turned in -1 wrapper per bar recieved from turn in.
      
              if (w==1){ //break out of infinite loop when you get 1 bar for 1 wrapper!
                  System.out.print("Infinite Bars, exiting infinite loop at bars = ");
                  break;
              }
          }
          return bars;
      }
      //read input for each test case and make 2d array of the info
      public static int[][] readInput(Scanner sc, int t){
      
          int[][] input = new int[t][3];
      
          for (int i = 0; i<t; i++){
              String[] inputLine = sc.nextLine().split(" ");
      
              input[i][0] = Integer.parseInt(inputLine[0]);
              input[i][1] = Integer.parseInt(inputLine[1]);
              input[i][2] = Integer.parseInt(inputLine[2]);
          }
          return input;
      }
      

      }

      -3|
      ParentPermalink
    • xinzhg 4 years ago+ 0 comments

      Could you explain your code?

      0|
      ParentPermalink
    • jayparekh56 4 years ago+ 0 comments

      +1 Used the exact same logic with c++

      1|
      ParentPermalink
    • jayparekh56 4 years ago+ 0 comments

      +1 Used the exact same logic with c++

      0|
      ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature