Analysis and programming of practical algorithm--recursive method (oil storage point including algorithm analysis, code)

Source: Internet
Author: User

Reverse Push method

The so-called backward-push method is to know the initial value of the case, through some kind of recursive relationship to learn the solution or target of the problem, and then the upside down, deduce its initial conditions, because the operation of such problems is one by one mapping, it can be analyzed by the recursive formula. Then, starting from this solution or goal, we use the backward method to push backward to the initial statement of the problem.

Oil storage Point
A heavy-duty truck that travels through a 1000-kilometer desert with a truck with a fuel consumption of 1 liters/km and a truck with a total oil capacity of 500 litres is clearly not going to be in the desert. So the level four must try to set up several storage sites along the way so that trucks can cross the desert smoothly, how do drivers build these oil storage points? How much petrol should be stored at each oil storage point to allow the truck to pass through the desert at the cost of least petrol?

Algorithm analysis



The specific code is as follows:

/* * ===================================================================================== * * filename:recurs ion.cc * * Description: Oil storage point, a heavy truck to cross the 1000 km desert, the truck consumes 1 liters/* km, and the truck's total oil capacity is 500 litres. The driver managed to set up several oil storage points along the way so that the truck could cross the desert and ask the driver how to set up the fuel storage point. How much petrol should be stored at each oil point in order to allow the truck to pass at the cost of minimum petrol consumption? * * version:1.0 * CREATED:04/25/15 08:57:38 * revision:none * COMPILER:GCC * * A  Uthor:xiu * Organization: * * ===================================================================================== */#include <iostream>using namespace STD;Const intN = -;//n for the set of oil storage pointsConst floatDistance = +;//Total length of desertConst intCapacity = -;//Truck oil carrying capacity//Output The information of each oil storage point, distance distance is the distance from the end pointvoidOutfloat*distance,float*oil,intcur) {cout<<"Output the information of each oil storage point, distance distance is the distance from the end point"<<endl;cout<<"No\tdistance\toil\n"; for(inti =0; i < cur; i++) {cout<< i +1<<"\ T"<< Distance[i] <<"\t\t"<< Oil[i] <<"\ n"; }}//recursion distance and oil storage capacityvoidRecursion (float*distance,float*oil,intCurfloatDis_sum) {if((dis_sum-distance) >=0.0001) {Out (distance, oil, cur);return; }Else{Oil[cur] = (cur+1) * capacity; Distance[cur] = Dis_sum + ((float) capacity)/(2* cur +1); Recursion (distance, oil, cur +1, Distance[cur]); }}intMain () {floatDistance[n] = {0};floatOil[n] = {0}; Recursion (distance, oil,0,0.0);return 0;}

Analysis and programming of practical algorithm--recursive method (oil storage point including algorithm analysis, code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.