Reading notes for "Analysis and Program Design of Practical Algorithms" (Day 1)

zhaozj2021-02-16  44

Due to the recommendation of many netizens, I finally made me calm down and see the book of "Analytical Algorithm Analysis and Programming"! Sure enough, it is in fact! Now use C issues in the process of reading the book, the original text is given by pascal! Many friends have given up this book because of this reason! It is a pity! Note: I have passed in BC5.0 in BC5.0!

Reproduction page 4

A heavy truck wants to pass 1 delete kilometers, the truck fuel consumption is 1 liter / km, the total power of the truck is 500

liter. Obviously the truck is installed, it can't be deserted. Therefore, the driver must try to establish several oil reservoirs along the way.

Make the truck smoothly through the desert, ask the driver how to build these reservoirs? How much gasoline should be stored for each reservoir point

Enable trucks to consume minimal petrol through the desert?

answer:

#include

#include

Void Oil_Lib ()

{

INT K; Float D, DL;

Float Oil [10], DIS [10];

Cout << "NO." << SETW (20) << "DISTANCE (K.M)" << SETW (90) << "OIL (L.) << endl;

K = 1;

D = 500;

DIS [1] = 500;

Oil [1] = 500;

Do {

K ;

D = 500 / (2 * k-1);

DIS [K] = D;

OIL [K] = OIL [K-1] 500;

WHILE (D <1000);

DIS [K] = 1000;

DL = 1000-DIS [k-1];

OIL [K] = DL * (2 * K 1) Oil [k-1];

For (int i = 0; i

COUT << I << SETW (20) << (1000-DIS [K-I]) << SETW (90) << Oil [K-I] << ENDL;

}

Piggy Water Page 10 There is a thief when theft is found in a store: the Item value VI Yuan, heavy Wi pound, (1 <= i

<= N), this VI and WI are integers. He hopes that the thing that is taken is, the better, but his backpack can only

Pack the W pound thing (W is an integer).

If you allow thieves to take a part of a certain item, what few things should be taken away, everything

What is the weight? Question: #include #include const Maxn = 1000; Class Node {public: node () {} Node (int A, float b, float c): Num a), w (b), v (c) {vper = c / w;} int Num; float w, v, vper;}; node list [maxn], lt [max]; void print (int N) { For (int i = 0; i r) || (List [i] .vper> = list [j] .vper))) {lt [t] = list [i]; i ;} else {LT [t] = list [j]; j ;} T ;} for (int S = p; s <= r; s ) list [s] = lt [s];} void merge_sort (int P, int R) { INT Q; if (p! = r) {q = (p r) / 2; merge_sort (p, q); merge_sort (q 1, r); merge (p, q, r);}} void partial_bag_problem (int N, float w) {float v = 0; float w, v; for (int i = 0; i > W >> V; Node Node ((i 1), w, v); list [i] = node;} / * print (n) * / merge_sort (0, n-1); / * print (n) * / int J = 0 WHILE (W> List [J] .w && j > N >> W; partial_bag_problem (n, w); return 1;} Task Scheduling Problem A unit Time Task is a job, if you want to run a program on your computer, it just covers a unit

operation hours. A set S for a set of unit time tasks, an arrangement of one scheduling of S, which specifies the execution order of these tasks. The first task in this scheduling begins at time 0, ending at the second task of time 15

At time 1, the end of time 2; .......

The inputs of the unit time task scheduling problem with time limit and fine on single processors are as follows:

1. Contains a collection of N unit time tasks S = F1, 2, ..., N75

2. n Taken the deadlines D1, I. . ..., D n, (1

3.21 non-negative rights (or fine) W:, ·, b ..., wno if the task I ends before time Di

Fine W5 ;.

It is required to find a scheduling of S, minimizing the total fines.

Question: #include const marx = 500; class node {public: node () {} node (int A, int B, INT C): K (a), d (b), w (c) {} INT K, D, W;}; Node List [MAXN], LT [MAXN]; Void Print (INT N) {for (int i = 0; i r) || (List [i]. W> = List [j] .w))))) LT [t] = list [i ]; Else LT [T] = List [J ]; T ;} for (int S = P; s <= R; s ) List [s] = lt [s];} void merge_sort (int P, int r) {INT Q; if (p! = r) {q = (p r-1) / 2; merge_sort (p, q) ; Merge_sort (q 1, r); merge (p, q, r);}} void Tasks_with_limit_and_fine (int N) {INT D, W; INT PCK [MAXN]; int Num = 0, T, SUM = 0; / * The current number of tasks is fine * / for (INT i = 0; i > D >> W; Node Node ((i 1), d, w); List [ I] = node;} / * print (n); * / merge_sort (0, n-1); / * Print (n); * / int I, j; for (i = 0; i 0) {if (list [pck [j]]. d

转载请注明原文地址:https://www.9cbs.com/read-27002.html

New Post(0)