Table of Contents
Reply content:
Home Backend Development PHP Tutorial Use recursive functions to solve the following problems

Use recursive functions to solve the following problems

Dec 01, 2016 am 01:27 AM
php

  1. 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?

    1. 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?

    1. Use recursion to output the following numbers

    5, 3, 1, 3, 5

Reply content:

  1. 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?

    1. 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?

    1. 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>
Copy after login

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles