/home/toolbox/public_html/solutions/101/10114/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9 #include <ctype.h>
10
11 #define TRUE (1 == 1)
12 #define FALSE (1 != 1)
13
14 #define DEBUG if (FALSE)
15
16 /*
17 * Author: Isaac Traxler
18 * Date: 2025-04-15
19 * Purpose: fun
20 * Problem: 10114 - Loansome Car Buyer
21 */
22
23 /*
24 * This template reads data until a terminating value is reached.
25 */
26
27 #define MAX_MONTHS 102
28 double rates[MAX_MONTHS];
29 int duration;
30 double down;
31 double loan;
32 int numRates;
33 double value;
34
35 void init()
36 {
37 /* FUNCTION init */
38 } /* FUNCTION init */
39
40 void dump()
41 {
42 /* FUNCTION dump */
43 } /* FUNCTION dump */
44
45 int getInput()
46 {
47 /* FUNCTION getInput */
48 int dataReadFlag = FALSE;
49 int in_months;
50 double in_rates;
51 double prev_rates;
52 int i;
53 int j;
54 int k;
55
56 scanf(" %d %lf %lf %d ", &duration, &down, &loan, &numRates);
57 if (0 <= duration)
58 {
59 /* valid duration */
60 dataReadFlag = TRUE;
61 value = loan + down;
62 k = 0;
63 scanf(" %d %lf ", &in_months, &in_rates);
64 rates[0] = in_rates;
65 for (i=1; i<numRates; i++)
66 {
67 /* get each depreciation rate */
68 prev_rates = in_rates;
69 scanf(" %d %lf ", &in_months, &in_rates);
70 for (j=k; j<in_months; j++)
71 {
72 /* set monthly rates */
73 rates[j] = prev_rates;
74 } /* set monthly rates */
75 k = in_months;
76 } /* get each depreciation rate */
77 /* now take care of payments from last stated month to loan duration */
78 for (j=k; j<=duration; j++)
79 {
80 /* set monthly rates */
81 rates[j] = in_rates;
82 } /* set monthly rates */
83 } /* valid duration */
84 return (dataReadFlag);
85 } /* FUNCTION getInput */
86
87 void process()
88 {
89 /* FUNCTION process */
90 int i;
91 double loss;
92 double loanPayment;
93
94 DEBUG printf(" %d: (value: %lf) (loan: %lf)\n", 0, value, loan);
95 loanPayment = loan / duration;
96 loss = value * rates[0];
97 value = value - loss;
98
99 DEBUG printf(" %d: (value: %lf) (rate: %lf) (loan: %lf)\n", 0, value, rates[0], loan);
100 for (i=1; (i<=duration)&&(loan>value); i++)
101 {
102 /* for each month */
103 loan = loan - loanPayment;
104 loss = value * rates[i];
105 value = value - loss;
106 DEBUG printf(" %d: (value: %lf) (rate: %lf)(loan: %lf)\n", i, value, rates[i], loan);
107 } /* for each month */
108 i--;
109 printf("%d month", i);
110 if (1 != i)
111 {
112 printf("s");
113 }
114 printf("\n");
115
116 } /* FUNCTION process */
117
118 int main()
119 {
120 /* main */
121 int moreToDo;
122
123 init();
124 moreToDo = getInput();
125 while (moreToDo)
126 {
127 /* while */
128 process();
129 moreToDo = getInput();
130 } /* while */
131
132 return EXIT_SUCCESS;
133 } /* main */
134