fork download
  1. //NSH 12/6/16
  2. //code for "Carousel Rides" kattis problem
  3.  
  4.  
  5.  
  6. import java.util.*;
  7. import java.lang.*;
  8. import java.io.*;
  9.  
  10. class CarouselRides
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. Scanner scan = new Scanner(System.in);
  15. int numOffers = scan.nextInt();
  16. int maxTickets = scan.nextInt();
  17. double minRate = Double.POSITIVE_INFINITY;
  18.  
  19. while(numOffers != 0){
  20.  
  21. int numTickets = 0;
  22. int dollars = 0;
  23. for (int i = 0; i < numOffers; i++) {
  24. int tickets = scan.nextInt();
  25. int money = scan.nextInt();
  26. double rate = (double)money/tickets;
  27. if(tickets <= maxTickets && minRate >= rate){
  28. if(minRate != rate || tickets > numTickets){
  29. minRate = rate;
  30. numTickets = tickets;
  31. dollars = money;
  32. }
  33. }
  34. }
  35.  
  36. if(minRate != Double.POSITIVE_INFINITY){
  37. System.out.println("Buy " + numTickets + " tickets for $" + dollars);
  38. }
  39. else {
  40. System.out.println("No suitable tickets offered");
  41. }
  42.  
  43. numOffers = scan.nextInt();
  44. maxTickets = scan.nextInt();
  45. minRate = Double.POSITIVE_INFINITY;
  46. }
  47.  
  48. }
  49.  
  50. }
  51.  
Success #stdin #stdout 0.06s 711680KB
stdin
3 5
1 3
3 5
4 7
3 2
3 5
1 3
4 7
3 2
3 6
1 2
2 4
1 3
4 10
0 0
stdout
Buy 3 tickets for $5
Buy 1 tickets for $3
Buy 2 tickets for $4
No suitable tickets offered