1. 题目源地址:http://poj.org/problem?id=2709
2. 解题思路:
每个颜料盒可能有3-12种颜色,其中每种颜色50ml。任意三种颜色(假设每种颜色Xml)可以混合出Xml的灰色。现在给出所需颜色的种数N,给出N个值分别代表每种颜色所需量,最后给出需要的灰色的量。问最少需要多少个颜料盒可以得到上述的颜色。
注意:假设数据为:4 90 95 75 95 10。也就是需要颜色数为4(蕴含着你取得颜料盒中只有四种颜色,而不是每个颜料盒都有12个颜色的),分别需要90,95,75,95,灰色需要10。
3.解题代码:
//POJ--2709:Painter
#include<iostream>
#include<algorithm>
using namespace std;
int cmp(int a,int b)
{
return a > b;
}
int main()
{
int N,ans,gray,temp,i;
int color[15];
while(cin>>N && N)
{
for(i=0;i<N;i++)
cin>>color[i];
cin>>gray;
sort(color,color+N);//将所需颜色量从小到大排序
ans=0;
//计算除了灰色之外一共需要多少颜料盒
if(color[N-1]%50==0)
ans=color[N-1]/50;
else
ans=color[N-1]/50+1;
for(i=0;i<N;i++)
color[i]=50*ans-color[i];//剩余颜色量从大到小排序
temp=0;//合成灰色的量
while(temp<gray)
{
if(color[2]==0)//当三种颜色中的最小值等于0时,需要添加一个颜料盒
{
for(i=0;i<N;i++)
color[i]+=50;
ans=ans+1;
}
color[0]--;
color[1]--;
color[2]--;
temp++;
sort(color,color+N,cmp);//每次都将个个颜色的量从大到小排序
}
cout<<ans<<endl;
}
return 0;
}