题意:一套涂料有3~12种颜色,每种颜色50ml。Emily上课需要n种颜色的涂料,第i种颜色需要color[i]ml,此外,Emily还需要gray ml的灰色涂料,每ml灰色的涂料需要3种不同颜色的其他涂料各1ml融合而成。问emily要上课,至少需要买几套涂料?
思路:贪心。由于n很小,所以每次1ml的其他涂料融合成灰色时,再对他们进行排序。
代码如下:
#include<iostream>
#include<algorithm>
using namespace std;
const int Max = 15;
int main()
{
int n, i, color[Max], gray;
while(cin >> n && n != 0)
{
for(i = 0; i < n; i ++)
cin >> color[i];
cin >> gray;
sort(color, color + n);
int ans = 0, max = 0;
while(max < color[n-1])
{ // 首先找去满足除了灰色的其他颜色,最少需几套涂料。
ans ++;
max += 50;
}
while(1)
{
while(color[2] < max && gray > 0)
{ // 模拟3种不同颜色融合成灰色的情况。
color[0] ++;
color[1] ++;
color[2] ++;
gray --;
sort(color, color + n);
}
if(gray == 0)
break;
ans ++;
max += 50;
}
cout << ans << endl;
}
return 0;
}