016-Shopping List

Source: Internet
Author: User

/* 016-Shopping List Topic description Wang Qiang is very happy today, the company issued N Yuan year-end award. Wang Qiang decided to use the year-end award for shopping, he would like to buy items divided into two categories: the main parts and accessories, the attachment is from one of the main parts, the following table is some examples of main parts and accessories: Main accessories computer printer, scanner bookcase book desk lamp, stationery work Chair No if you want to buy items classified as attachments, You must first buy the main part that the attachment belongs to. Each main piece can have  0 ,  1 , or  2  attachments. Attachments no longer have attachments that belong to them. Wang Qiang want to buy a lot of things, in order to not exceed the budget, he set each item an important degree, divided into  5 , etc.: With integer  1 ~ 5 , the first  5  and so on the most important. He also found the price of each item (all multiples of  10 ) from the Internet. He hoped that the sum of the product of the price and the importance of each item would be the largest if not more than  N  yuan (can equal to  N  yuan).      the price of the article  j  items is  v[j]&nbsp, and the important degree is  w[j]  the  k  items are selected in order  j 1 , j 2 ,......, j k , then the sum to be asked is: v[j 1 ]*w[j 1 ]+ v[j 2 ]*w[j 2 ]+ ... +v[j k ]*w[j k ] . (where  *  is multiplication sign)      please help Wang Qiang design a shopping list to meet the requirements. Input Description: Enter the  1  line, which is two positive integers separated by a space: N m (where  N  ( <32000 ) represents the total amount of money, m  (  <60 ) is the number of items you wish to purchase. From line  2  to section  m+1 , section  j  line gives the number &nbsBasic data for p;j-1  items, with  3  non-negative integer  v p q per line (where  v  represents the price of the item ( v<10000 ) , p  indicates that the item's importance ( 1 ~ 5 ), q  Indicates whether the item is a main piece or an accessory. If  q=0&nbsp, indicates that the item is a main piece, if  q>0&nbsp, indicating that the item is an attachment, q  is the number of the owning main part)   Output Description:  output file has only one positive integer, The maximum value ( <200000 ) for the sum of the price and the importance of the item that does not exceed the total amount of money. Input example: 1000 5800 2 0400 5 1300 5 1400 3 0500 2 0 Output Example: 2200 *///C language edition #include <stdio.h>int main (void) {int n,m;//n is the year-end award, M is the number of items you want to purchase int v[60];// Store input Item Price  int p[60];//Store the importance of the input item int q[60];//store the attribute value of the input item, whether it is an attachment or a main part int f[60][32000]; // The value that stores the product of the price and the importance, as the output  int i,j;//accepts input  scanf ("%d %d", &n,&m); for (i = 1;i  <= m;i++) scanf ("%d %d %d", &v[i],&p[i],&q[i]);//processing data for (i = 1;i  <= m;i++ )         p[i] = v[i] * p[i]; &nbSp;   for (i=1;i<=n;i++)     {         for (j=1;j<=m;j++)         {             if (q[j]==0)              {                 if (I<v[j])                      f[j][i]=0;                 else                 {                     if (F[j-1][i-v[j]]+p[j]>f[j-1][i])                          f[j][i]=f[j-1][i-v[j]]+p[j];                      else                         f[j][i]=f[j-1][i];                 }             }            else{                     if (I<v[j]+v[q[j])                              f[j][i]=0;                     else                      {                          if (F[j-1][i-v[j]]+p[j]>f[j-1][i])                               f[j][i]=f[j-1][i-v[j]]+p[j];                         else                              f[j][i]=f[j-1][i];                     }              }        }    }printf ("%d\n", F[m][N]); return  0; }


This article is from the "Similarities" blog, please be sure to keep this source http://yutianheji.blog.51cto.com/9580523/1829153

016-Shopping List

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.