fork(5) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class LoansomeCarBuyer
  8. {
  9. public static void main (String[] args)
  10. {
  11. // Input
  12. Scanner scanner = new Scanner(System.in);
  13. int term = scanner.nextInt();
  14. while (term >= 0) {
  15.  
  16. double downPayment = scanner.nextDouble();
  17. double loan = scanner.nextDouble();
  18.  
  19. double initialValue = downPayment + loan;
  20. double payment;
  21. if (term != 0) {
  22. payment = loan / term;
  23. } else {
  24. payment = loan;
  25. }
  26.  
  27. int numRecords = scanner.nextInt();
  28.  
  29. int[] months = new int[numRecords];
  30. double[] values = new double[numRecords];
  31.  
  32. for (int i = 0; i < numRecords; i++) {
  33. months[i] = scanner.nextInt();
  34. values[i] = scanner.nextDouble();
  35. }
  36.  
  37. // Calculation.
  38. double prev = values[0];
  39. double currentValue = initialValue - initialValue * prev;
  40. double owe = loan;
  41. int currentMonth;
  42.  
  43. if (currentValue > owe) {
  44. currentMonth = 0;
  45. } else {
  46. int i = 1;
  47. currentMonth = 1;
  48.  
  49. while (true) {
  50.  
  51. if (i < numRecords && currentMonth == months[i]) {
  52. prev = values[i];
  53. i++;
  54. }
  55.  
  56. currentValue = currentValue - currentValue * prev;
  57. owe -= payment;
  58.  
  59. if (currentValue > owe) break;
  60.  
  61. currentMonth++;
  62. }
  63. }
  64.  
  65. if (currentMonth == 1) {
  66. System.out.println(currentMonth + " month");
  67. } else {
  68. System.out.println(currentMonth + " months");
  69. }
  70.  
  71. term = scanner.nextInt();
  72. }
  73. scanner.close();
  74. }
  75. }
Success #stdin #stdout 0.13s 4386816KB
stdin
30 500.0 15000.0 3
0 .10
1 .03
3 .002
12 500 9999.99 2
0 .05
2 .1
60 2400.0 30000.0 3
0 .2
1 .05
12 .025
100 0 60000 101
0 .1
1 .1
2 .01
3 .01
4 .1
5 .1
6 .01
7 .01
8 .01
9 .01
10 .1
11 .01
12 .01
13 .01
14 .01
15 .01
16 .01
17 .01
18 .01
19 .01
20 .1
21 .01
22 .01
23 .01
24 .01
25 .01
26 .01
27 .01
28 .01
29 .01
30 .1
31 .01
32 .01
33 .01
34 .01
35 .01
36 .01
37 .01
38 .01
39 .01
40 .1
41 .01
42 .01
43 .01
44 .01
45 .01
46 .01
47 .01
48 .01
49 .01
50 .1
51 .01
52 .01
53 .01
54 .01
55 .01
56 .01
57 .01
58 .01
59 .01
60 .1
61 .01
62 .01
63 .01
64 .01
65 .01
66 .01
67 .01
68 .01
69 .01
70 .1
71 .1
72 .1
73 .1
74 .1
75 .01
76 .01
77 .01
78 .01
79 .01
80 .1
81 .1
82 .1
83 .1
84 .1
85 .1
86 .1
87 .1
88 .1
89 .1
90 .1
91 .1
92 .1
93 .01
94 .01
95 .01
96 .1
97 .1
98 .1
99 .1
100 .1
100 0 75000 2
0 .10
99 .5
100 74999 1 1
0 .20
36 2000 10000 4
0 .2
1 .1
3 .01
10 .005
36 0 12000 3
0 .25
1 .1
10 .005
23 3000 5000 2
0 .15
20 .005
42 4000 0 1
0 .79
-10 0 0 4
stdout
4 months
1 month
49 months
99 months
100 months
0 months
11 months
27 months
0 months
0 months