Home > Article > Backend Development > Use recursive functions to solve the following problems
Zhang San recharged the gas card with 20,000 yuan at the beginning of the year. It is known that Zhang San refuels 4 times a month, and the cost is 350*4 yuan.
Every time Zhang San refuels 10 times, the gas station will offer a 20% discount and a 10% cashback. Question: How many times does Zhang San’s card need to be recharged after refilling? How much is the balance on the fuel card after each refueling?
Xiao Wang’s parents saved him 20,000 yuan for living expenses when he started school. It is known that Xiao Wang will withdraw 1,500 yuan every month after the bank calculates interest. Assume that the monthly interest rate on demand is 0.32%.
So what is Xiao Wang’s balance after each withdrawal? How many times has Xiao Wang finished taking it?
Use recursion to output the following numbers
5, 3, 1, 3, 5
Zhang San recharged the gas card with 20,000 yuan at the beginning of the year. It is known that Zhang San refuels 4 times a month, and the cost is 350*4 yuan.
Every time Zhang San refuels 10 times, the gas station will offer a 20% discount and a 10% cashback. Question: How many times does Zhang San’s card need to be recharged after refilling? How much is the balance on the fuel card after each refueling?
Xiao Wang’s parents saved him 20,000 yuan for living expenses when he started school. It is known that Xiao Wang will withdraw 1,500 yuan every month after the bank calculates interest. Assume that the monthly interest rate on demand is 0.32%.
So what is Xiao Wang’s balance after each withdrawal? How many times has Xiao Wang finished taking it?
Use recursion to output the following numbers
5, 3, 1, 3, 5
First question:
<code><?php $total = 20000; function myoil($total,$times=0){ $times++; if ($times%10 !=0) { $total = $total - 350; }else{ $total = $total -350*0.7; //打8折再返现1折,理解为打7折了 } echo $times."次,还剩".$total."<br>"; if (($times%10 !=0 && $total<350) || ($times%10 ==0 && $total<350*0.7)) { return false; }else{ myoil($total,$times); } } myoil($total); ?> </code>
Results:
1 time, 19650
2 times left, 19300
3 times left, 18950
4 times left, 18600
5 times left, 18250
6 times left, 17900
7 times left, still 17550
8 times left, 17200
9 times left, 16850
10 times left, 16605
11 times left, 16255
12 times left, 15905
13 times left, 15555
14 times left, left 15205
15 times, 14855
16 times left, 14505
17 times left, 14155
18 times left, 13805
19 times left, 13455
20 times left, 13210
21 times left, 12860 left
22 times, 12510 left
23 times, 12160 left
24 times, 11810 left 29 times, 10060
30 times left, 9815
31 times left, 9465
32 times left, 9115
33 times left, 8765
34 times left, 8415
35 times left, 8065
36 left times, there are 7715
37 times left, 7365
38 times left, 7015
39 times left, 6665
40 times left, 6420
41 times left, 6070
42 times left, and 5720
43 times left. , there are 5370
44 times left, 5020
45 times left, 4670
46 times left, 4320
47 times left, 3970
48 times left, 3620
49 times left, 3270
50 times left, There are 3025
51 times left, 2675
52 times left, 2325
53 times left, 1975
54 times left, 1625
55 times left, 1275
56 times left, 925
57 times left, and 575
58 times left, 225
left
The following principles are the same