Analysis and programming of practical algorithm--recursive method (inverted method)

Source: Internet
Author: User

In the case of unknown initial value, the backward-pushing method learns the solution or goal of the problem by some kind of recursive relation, and then the initial condition is deduced. Because the operation process of this kind of problem is one by one mapping, it can analyze its recursive formula, then from this solution or the target, use the backward push means, step backward to the initial statement of the problem. Oil storage point A heavy truck wants to cross 1000 km of desert, the truck consumes 1 liters/km, the truck's total oil capacity is 500 liters, it is clear that the truck is not able to fly once oil is not in the desert, so four must try to set up a few oil storage points along the way, so that the truck can cross the desert, how the driver to 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




Code
/* * ===================================================================================== * * 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 (inverted method)

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.