Reading notes for the analysis and program design of Practical Algorithms (1th Day) __ algorithm

Source: Internet
Author: User

Because many netizens ' recommendation, finally let me calm down to take a good look at the "Practical Algorithm Analysis and Program design" this book. Indeed, it is the name of the bill. Now will I read the course of the problems encountered in C + + given, the original text is given by Pascal. Many friends even gave up the book for this reason. It's a pity. Note: I have been Bc5 these programs. 0 was passed.

Recursion Page 4th

    A heavy-duty truck was trying to get through 1 of kilometres of desert, and trucks consumed 1 liters/km, and truck presidents had oil capacity of up to a
litre. It is obvious that the truck can not go through the desert before loading oil. So the driver has to try to set up several storage sites along the
way so that the trucks can cross the desert smoothly. How much petrol should be stored at each storage point to
allow the truck to pass through the desert at the cost of least petrol?
    Exercises
     	#include <iostream.h>
	#include <iomanip.h>
	void Oil_lib ()
{
	
	float OIL[10],DIS[10];
 	cout<< "No." <<SETW << "Distance (K.M)" <<SETW (+) << "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<k;i++)
		COUT<<I<<SETW (<<) (1000-dis[k-i]) <<SETW (+) <<oil[k-i]<<endl;
}
Greedy Law, page 10th
    There was a thief who stole a shop and found N items: item I value VI, heavy WI-lb, (1<=i
<=n), where  VI and WI are integers. He hopes to take away the more valuable the better, but his backpack can only be
loaded with W-pounds of Things (W is an integer).

    If a thief is allowed to take a part of an item, what items should the thief take away, and what
is the weight of each thing?
	Exercises
#include <iostream.h>
#include <stdio.h>
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[MAXN];
void print (int n)
{
for (int i=0;i<n;i++)
cout<<list[i].num<< "" <<list[i].w<< "" <<list[i].v<<endl;
cout<<endl;
}
void merge (int p,int q,int R)
{
int i,j,t;
t=p;i=p;j=q+1;
while (t<=r) {
if ((i<=q) && ((j>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<n;i++) {/* Item weight and value */
cin>>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-=LIST[J].W;
V+=LIST[J].V;
printf ("%d%8.2f%8.2f/n", LIST[J].NUM,LIST[J].W,LIST[J].V);
j + +;
}
if (j<n&&w!=0) {
V+=w*list[j].vper;
printf ("%d%8.2f%8.2f/n", list[j].num,w,w*list[j].vper);
w=0;
}
cout<< "Total value:" <<V<<endl;
}
int main ()
{
int n,w;
cin>>n>>w;
Partial_bag_problem (N,W);
return 1;
}
Greedy Law, page 15th
Task scheduling problem
    a unit-time task is a job, such as running a program on a computer that covers the run time of a unit
. Given a set of unit time tasks, a dispatch of S is an arrangement of S, which specifies the
order in which these tasks are executed. The first task in the schedule starts at time 0, ends at time 15 The second task starts
at time 1, ends at time 2; The
    unit time task scheduling problem with deadlines and fines on a single processor is as follows:
    1. A collection that contains N unit time tasks S=f1,2,......,n75
    2. The term of n rounding D1,i. ..., d N, (1
  
	Exercises
#include <iostream.h>
Const MAXN=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<n;i++)
cout<<list[i].k<< "" <<list[i].d<< "" <<list[i].w<<endl;
cout<<endl;
}
void merge (int p,int q,int R)
{
int i,j,t;
t=p,i=p,j=q+1;
while (t<=r) {
if ((i<=q) && ((j>r) | | (LIST[I].W&GT;=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; /* Total fine number of tasks currently completed */
for (int i=0;i<n;i++) {
cin>>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<n;i++) {
t=0;
for (j=0;j<num;j++)
if (list[pck[j]].d<=num)
t++;
/* cout<< "T:" <<t<< ""; */
if (T&LT;LIST[I].D) {
Pck[num]=i; LIST[I].K=-LIST[I].K;    j=num++; /*cout<< "J:" <<j<< "; * *
while (j>0) {
if (LIST[PCK[J]].D&LT;LIST[PCK[J-1]].D) {
T=PCK[J];p ck[j]=pck[j-1];p ck[j-1]=t;
}
j--;
}
}
}
for (i=0;i<num;i++)
cout<< (-LIST[PCK[I]].K) << "";
cout<<endl;
for (i=0;i<n;i++)
if (list[i].k>0) {
cout<<list[i].k<< "";
SUM+=LIST[I].W;
}
cout<<endl;
cout<< "Total fine=" <<sum<<endl;
}
int main ()
{
int N;
cin>>n;
Tasks_with_limit_and_fine (N);
return 1;
}

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.