C language learning fun _ data structure _ classic proposition _ 1 _ backpack question _ Analysis _ 1

Source: Internet
Author: User

 

Yesterday I tested how to use a function to apply for a space from the stack space of the Program for other functions.

 

Data Structure proposition: the knapsack problem.

 

The proposition is as follows:

 

View Code

/* 1. Problem description assume there is a backpack with a total volume of T and n pieces with a volume of w1, w2 ,... Wn items, can you pick several items from n items that are exactly filled with backpacks, even if w1 + w2 +... + Wm = T. All solutions that meet the preceding conditions must be found. For example, when T = 10, the size of each item is {,}, the following four groups of solutions can be found: (,) (, 5) (3, 5, 2 ). 2. Implementation tips can be used to solve the knapsack problem with the design philosophy of the Backtracking Method. First, sort items into a column, and then select items in sequence to load them into a backpack. If you have selected items under I, continue to select items under I + 1, if this item is too large to be loaded, discard it and continue to select the next item until the backpack is full. If no suitable item can be found in the remaining items to fill the backpack, it indicates that the loaded item is "inappropriate" and should be taken out of the "discard side ", continue to select from the item after "it" until a solution that meets the conditions is obtained or no solution is obtained. Because the rule for backtracking is "post-import, first-out", "stack" is used ". 3. Further consideration: if each item has a volume and value, and the backpack has a size limit, the solution is to solve the problem that the total value of the items stored in the backpack is the greatest-the optimal solution or the approximate optimal solution. */

Today, the test is conducted according to the method in it. Some code has been implemented and some results can be tested, but not fully implemented yet,

 

Now I want to post the code for comments. I wanted to get it out today, but it's a bit late because I have to go to work tomorrow.

 

It's already half past seven ,).

 

We will continue to implement the remaining parts tomorrow.

 

View Code

/* 1. Problem description assume there is a backpack with a total volume of T and n pieces with a volume of w1, w2 ,... Wn items, can you pick several items from n items that are exactly filled with backpacks, even if w1 + w2 +... + Wm = T. All solutions that meet the preceding conditions must be found. For example, when T = 10, the size of each item is {,}, the following four groups of solutions can be found: (,) (, 5) (3, 5, 2 ). 2. Implementation tips can be used to solve the knapsack problem with the design philosophy of the Backtracking Method. First, sort items into a column, and then select items in sequence to load them into a backpack. If you have selected items under I, continue to select items under I + 1, if this item is too large to be loaded, discard it and continue to select the next item until the backpack is full. If no suitable item can be found in the remaining items to fill the backpack, it indicates that the loaded item is "inappropriate" and should be taken out of the "discard side ", continue to select from the item after "it" until a solution that meets the conditions is obtained or no solution is obtained. Because the rule for backtracking is "post-import, first-out", "stack" is used ". 3. Further consideration: if each item has a volume and value, and the backpack has a size limit, the solution is to solve the problem that the total value of the items stored in the backpack is the greatest-the optimal solution or the approximate optimal solution.

*/# Include <stdio. h>

# Include <stdlib. h>

Struct node {

Int sum;

Int I;

};

Typedef struct node;

Int getmemory (int ** p, int size)

{

If (0> = size) return NULL;

If (NULL = (* p = (int *) malloc (size * (sizeof (int )))))

Return 0;

Else

Return 1;

}

Int assignvalue (int * dest, int size)

{If (NULL = dest)

Return 0;

While (size> 0)

{

Scanf ("% d", dest );

Dest ++;

Size --;

}

Return 1;

}

Int solution (int * source, int volume, int size)

{

Int I, j;

// J is used to store the int * stack, which is cheaper than the first stack address;

// The requested stack space int * header;

NODE temp;

If (NULL = source | 0 = volume | 0 = size) return 0; if (NULL = (stack = (int *) malloc (size * sizeof (int ))))

Return 0;

Header = stack;

For (I = 0; I <size; I ++)

{

Stack = header;

// Every loop returns the stack to the first address j = 0;

// Each cycle sets the stack space usage to 0;

Temp. I = temp. sum = 0;

// Each cycle changes the sum and space Count value to 0;

// The number of times to be calculated for each operation. The size value needs to be verified in the first loop.

// Check the size-I value for the second loop

While (temp. I <= size-I)

{

J ++;

Temp. sum + = * (source + I + temp. I );

* Stack = * (source + I + temp. I );

Stack ++;

If (temp. sum = volume)

// Output when the obtained sum is equal to the volume size

{

While (j> 0)

{

Printf ("% d", * (stack-1 ));

J --;

Stack --;

}

Putchar ('\ n ');

// Jump out of the loop after the output is complete and continue to sum the data in the next round

}

If (temp. sum <volume)

// If the extracted value is smaller than the volume after it is added, the next value is taken for calculation.

{

Temp. I ++;

Continue;

}

If (temp. sum> volume)

// If the obtained value is greater than the volume, it is discarded and the data of the newly pressed stack is output to the stack.

{

Temp. sum-= * (source + I + temp. I );

Temp. I ++;

* Stack = 0;

J --;

Stack --;

Continue;

} // End of if

} // End of while

} // End of

Free (stack );

Free (header );

Return 1;

}

Int main (int argc, char * argv []) {

Int size;

Int volume;

Int * number;

Number = NULL;

Printf ("input the total number of the package :");

Scanf ("% d", & size );

Printf ("\ nEnter the elements in the package: \ n"); if (! Getmemory (& number, size) exit (1 );

If (! Assignvalue (number, size) exit (1 );

Puts ("\ nPlease enter the volum of the package :");

Scanf ("% d", & volume );

If (0 = solution (number, volume, size ))

Exit (1 );

Return 0;

}

The current Code has not considered the typographical problem of input and output. Let's wait until the entire problem is solved ...........

 

I found that sometimes the C language is really interesting and it is easy to implement some interesting problems.

 

The above code is not complete yet, but it is just a model. Later I will discuss the data structure proposition.

 

Thank you ............

 

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.