飯卡 hdu2546

來源:互聯網
上載者:User

http://acm.hdu.edu.cn/showproblem.php?pid=2546

// 很想0 1背包的問題,可是又有很大的變形,一開始以為是貪心,寫了一個好囧的代碼,wrong
// 又寫了一個不是很熟悉的0 1 背包, 又wrong。好好努力!!
#include<iostream>//2251214 2010-03-26 18:00:08 Accepted 2546 31MS 304K 557 B C++ 悔惜晟
#include<algorithm>
using namespace std;
int a[1002];
int f[1002];
bool hash[1002];
int main()
{
 int n, m, i, j, min;
 while(cin>>n && n != 0)
 {
  for(i = 0; i < n ; i++)
   cin>>a[i];
   cin>>m;
  sort(a, a+n);
  min = 0;
  for(i =0 ;i <= m -5; i++)
   hash[i] =0;
  hash[0] =1;
  for( i =0; i < n; i++)
  {
   for(j = m - 5; j >= 0; j--)//卡上的剩餘金額大於或等於5元,就一定可以購買成功(即使購買後卡上餘額為負),
                                       //否則無法購買(即使金額足夠) 解決問題的關鍵
   {
    if(hash[j] == 1)
    {
     hash[j + a[i] ] = 1;
     if(min  < j + a[i])
     {
      min = j + a[i];
     }
    }
   }
  }
  cout<<m - min<<endl;
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.