9 degree tutorial 25th questions

Source: Internet
Author: User

C language source code: [cpp] # include <stdio. h> # include <stdlib. h> # include <limits. h ># define maxsize 600 double lengthcmax, pricemin, falg; double cmax, length, davg; int n; typedef struct station {double price; double length;} station; station sta [maxsize]; int cmp (const void * a, const void * B) {station * aa = (station *) a; station * bb = (station *) B; return aa-> length> bb-> length? 1:-1;} int find (int I) {int j, fprice = INT_MAX; double price = INT_MAX; for (j = I + 1; j <n & sta [I]. length + lengthcmax> = sta [j]. length; j ++) {if (sta [j]. price <price & sta [j]. price <sta [I]. price) {price = sta [j]. price; fprice = j ;}}if (fprice <n & sta [fprice]. price <sta [I]. price) return fprice; else return-1;} int findmin (int I) {int j, fprice = INT_MAX; double price = INT_MAX; for (j = I + 1; j <n & sta [I]. length + lengthcmax> = Sta [j]. length; j ++) {if (sta [j]. price <price) {price = sta [j]. price; fprice = j ;}}if (fprice <n) return fprice; else return-1;} void pri (int I, double oil) {// Initial Value 0, 0 int j, k; j = find (I); // find a station with a lower distance from the I station than the oil price of the I station if (j! =-1) {// if k = I + 1 is found; while (k <= j) if (sta [k]. price <sta [I]. price) break; else k ++; // find the station closest to I that is lower than I oil price, and sail to the k station, clear pricemin + = (sta [k]. length-sta [I]. length)/davg-oil) * sta [I]. price; pri (k, 0);} else {// if (sta [I] Not found. length + lengthcmax> = sta [n]. length) // if the distance from the end point is within a box of oil, direct to the end point pricemin + = (sta [n]. length-sta [I]. length)/davg-oil) * sta [I]. price; else {j = findmin (I); // find the station with the lowest oil price within a box of oil from the station I, and fill it with oil, heading for station j, pricemin + = (cmax-oil )* Sta [I]. price; pri (j, cmax-(sta [j]. length-sta [I]. length)/davg) ;}}int main () {int I; while (scanf ("% lf % d", & cmax, & length, & davg, & n )! = EOF) {lengthcmax = cmax * davg; for (I = 0; I <n; I ++) scanf ("% lf", & sta [I]. price, & sta [I]. length); qsort (sta, n, sizeof (sta [0]), cmp); sta [n]. length = length; sta [n]. price = INT_MAX; for (I = 0; I <n; I ++) if (sta [I]. length + lengthcmax <sta [I + 1]. length) break; if (sta [0]. length! = 0) printf ("The maximum travel distance = 0.00 \ n"); else if (I <n) {if (sta [0]. length = 0) printf ("The maximum travel distance = %. 2lf \ n ", sta [I]. length + lengthcmax); else printf ("The maximum travel distance = 0.00 \ n");} else {pricemin = 0; pri (); printf ("%. 2lf \ n ", pricemin) ;}} return 0 ;}

Related Article

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.