2017 NetEase Autumn recruit--7, buy apples

Source: Internet
Author: User

Title Description: (dp problem )

small easy to go to the nearby shops to buy apples, treacherous traders used the bundle, only 6 bags per bag and 8 bags per bag (packaging is not detachable). But small easy now just want to buy exactly n apples, small easy to buy as little as possible to carry the number of bags. If you can't buy exactly n apples, small easy will not be purchased. Input Description:Enter an integer n, which means it's easy to buy n (1≤n≤100) ApplesOutput Description:output An integer indicating the minimum number of bags to buy, and if you can't buy exactly n apples then output-1 Input Example: -Output Example:3idea: DP problem. First Dp[i] = min (dp[i],dp[i-a[j]+1). Enter the total number of apples, the DP is initially sum, so that the last dp[sum] = sum does not have the result of just sum, otherwise input dp[sum]
1#include <iostream>2 using namespacestd;3 //Dp[i] = min (dp[i],dp[i-a[j]+1)4  5 intMain ()6 {7     intsum;8      while(cin>>sum)9     {Ten         inta[2] = {6,8}; One         intdp[ -]; Adp[0] =0;//is also the key point -          for(intI=1;i< -; i++) -         { theDp[i] =sum; -         } -          for(intI=0; i<=sum;i++) -         { +              for(intj=0;j<2; j + +) -             { +                 if(I>=a[j] && Dp[i-a[j]] +1<Dp[i]) A                 { atDp[i] = dp[i-a[j]]+1; -                 } -             } -         } -         if(Dp[sum] = =sum) -cout<<-1<<Endl; in         Else -cout<<dp[sum]<<Endl; to         return 0; +     } -}

2017 NetEase Autumn recruit--7, buy apples

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.