fork download
  1. import java.util.Scanner;
  2. import java.text.NumberFormat;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner fin = new Scanner(System.in);
  7.  
  8. NumberFormat fmt = NumberFormat.getCurrencyInstance();
  9.  
  10. while (true) {
  11. double m1 = fin.nextDouble();
  12. if (m1 < 0) break;
  13. double m2 = fin.nextDouble();
  14. double m3 = fin.nextDouble();
  15.  
  16. int N = fin.nextInt();
  17.  
  18. if (N == 1)
  19. System.out.println("Month " + N + " cost: " + fmt.format(m1));
  20. else if (N == 2)
  21. System.out.println("Month " + N + " cost: " + fmt.format(m2));
  22. else if (N == 3)
  23. System.out.println("Month " + N + " cost: " + fmt.format(m3));
  24. else {
  25. for (int i = 3; i < N; i++) {
  26. double newVal = Math.round(m1*m2/m3*100.0)/100.0;
  27. m1 = m2;
  28. m2 = m3;
  29. m3 = newVal;
  30. }
  31. System.out.println("Month " + N + " cost: " + fmt.format(m3));
  32. }
  33. }
  34. }
  35. }
  36.  
Success #stdin #stdout 0.1s 2184192KB
stdin
5.23 5.50 5.52 4
5.23 5.50 5.52 2
2177.58 2713.7 433.11 1
2177.58 2713.7 433.11 2
2177.58 2713.7 433.11 3
1.00 1.00 300.00 4
2730.09 857.91 1657.49 23
1848.88 834.74 883.96 8
2139.59 1673.18 789.1 92
842.52 754.73 546.37 12
1661.62 583.08 2523.7 21
2323.23 446.56 1063.87 13
1609.55 2688.56 2446.8 6
1412.92 1182.61 1684.15 8
1474.11 1717.61 1474.11 34
685.29 2890.05 1655.34 54
2337.99 2490.45 618.13 11
1828.85 2307.46 892.15 20
1827.48 1325.26 1379.61 6
1952.23 2778.66 1247.81 84
551.16 780.3 2581.95 8
1652.54 2998.47 595.65 58
2117.78 1434.75 2304.84 81
308.16 2750.43 827.52 51
1583.25 172.82 1244.89 24
-1
stdout
Month 4 cost: $5.21
Month 2 cost: $5.50
Month 1 cost: $2,177.58
Month 2 cost: $2,713.70
Month 3 cost: $433.11
Month 4 cost: $0.00
Month 23 cost: $11.28
Month 8 cost: $7,637.98
Month 92 cost: $357,991,898.40
Month 12 cost: $6,580.54
Month 21 cost: $108,439.07
Month 13 cost: $21.42
Month 6 cost: $1,163.40
Month 8 cost: $698.31
Month 34 cost: $1,717.61
Month 54 cost: $0.01
Month 11 cost: $3.02
Month 20 cost: $1,475,522.79
Month 6 cost: $2,325.39
Month 84 cost: $542,457,751.68
Month 8 cost: $7.59
Month 58 cost: $495,509,353.86
Month 81 cost: $62,535.51
Month 51 cost: $227,603,337.36
Month 24 cost: $2,433.23