2709 PAINTER—计141 吴胜男
Description
Thelocal toy store sells smallfingerpaintingkits with between three and twelve 50ml bottles of paint, each a differentcolor. The paints are bright and fun to work with, and have the useful propertythat if you mix X ml each of any three different colors, you get X ml of gray.(The paints are thick and "airy", almost like cake frosting, and whenyou mix them together the volume doesn't increase, the paint just gets moredense.) None of the individual colors are gray; the only way to get gray is bymixing exactly three distinct colors, but it doesn't matter which three. Yourfriend Emily is an elementary school teacher and every Friday she does afingerpaintingproject with her class. Given the number of different colors needed, the amountof each color, and the amount of gray, your job is to calculate the number ofkits needed for her class.
The input consists of one or more test cases, followed by a linecontaining only zero that signals the end of the input. Each test case consistsof a single line of five or more integers, which are separated by a space. Thefirst integer N is the number of different colors (3 <= N <= 12).Following that are N different nonnegative integers, each at most 1,000, thatspecify the amount of each color needed. Last is a nonnegative integer G <=1,000 that specifies the amount of gray needed. All quantities are in ml.
For each test case, output the smallest number of fingerpaintingkits sufficient to provide the required amounts of all the colors and gray.Note that all grays are considered equal, so in order to find the minimumnumber of kits for a test case you may need to make grays using differentcombinations of three distinct colors.
3 40 95 21 0
7 25 60 400 250 0 60 0 500
4 90 95 75 95 10
4 90 95 75 95 11
5 0 0 0 0 0 333
0
2 8 2 3 4
每套颜料包3-12种颜料(没有灰色),每种颜料50ml。任意取3种颜料(每种颜料x ml)混合即可得到x ml的灰色颜料(根据油漆的化学性质,不同颜色的油漆混合,体积不会增加,只会使油漆更加粘稠)。给定一套颜料包内颜料种类的数量,每种颜色的总量和所需灰色颜料的量。计算至少使用多少套颜料包。
输入一个或多个测试数据,最后输入0结束数据输入。
每行测试数据包括5个或5个以上整数,用空格隔开。
每行第一个整数是颜色种类的数量,接下来的整数是
每种颜色需要的量(均小于1000ml),每行最后一个数代表所需灰色的量,单位为ml.
问题解决思路:
#include <iostream>
using namespace std;
int main()
{
int colorNum;
cin >> colorNum;
while(colorNum!=0)
{
int grayNum;
int* color=new int[colorNum];
int kitsNum;
for(int i=0; i<colorNum; i++)
cin >> color[i];
cin >> grayNum;
int max=0;
for(int i=0; i<colorNum; i++)
{
if(max < color[i])
max = color[i];
}
kitsNum =max/50+(max%50==0?0:1);
int temp=kitsNum*50;
for(int i=0; i<colorNum; i++)
color[i]=temp-color[i];
while(grayNum!=0)
{
int max1=0,max2=1,max3=2;//最大的三个数的位置
for(int i=3; i<colorNum; i++)
{
if(color[i] > color[max1])
max1 = i;
else if(color[i] > color[max2])
max2 = i;
else if(color[i] > color[max3])
max3 = i;
}
if(color[max1]>0 && color[max2]!=0 && color[max3]!=0)
{
color[max1]--;
color[max2]--;
color[max3]--;
grayNum--;
}
else
{
kitsNum++;
for(int i=0; i<colorNum; i++)
color[i]+=50;
}
}
cout << kitsNum << endl;
cin >> colorNum;
}
}