poj2709

确实是水题一道,今天早上没吃饭,看题看了好半天

题意:
有个卖玩具的店里有一种类似颜料盒的东东,颜料盒的颜色种数从3到12不等,每种
颜料有50ml。有一种很特别的颜色叫做灰色,任何一种颜料盒里本来不存在这种颜色,
但是可以用任意3种不同的颜色混合而成。现在你需要N种颜色的颜料盒,并且每种颜
料的需求量也给定,包括灰色,让你求出需要的最少的该颜料盒的个数

思路:

如果有灰色,在前面的颜色中选择三种最小的,分别加1,灰色减一,直到灰色为零

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int color[25];
 8     while(1)
 9     {
10         int n,i;
11         cin>>n;
12         if(n==0) break;
13         for(i=0;i<n;i++)
14         {
15             cin>>color[i];
16         }
17         int grey;
18         cin>>grey;
19         sort(color,color+n);
20         while(grey--)
21         {
22             color[0]++;
23             color[1]++;
24             color[2]++;
25             sort(color,color+n);
26         }
27         sort(color,color+n);
28         int max=color[n-1];
29         int ans=(max%50==0)?(max/50):(max/50+1);
30         cout<<ans<<endl;
31     }
32     return 0;
33 }

贪心算法确实是一种很简单的算法,没什么难度,关键看按什么排序

posted on 2012-08-07 11:11  矮人狙击手!  阅读(265)  评论(0编辑  收藏  举报

导航