(一) 邮票问题(Problem Set 1746):
1. 问题描述
设有n种不同面值a1, a2,…, an的邮票,规定每封信最多贴m张邮票。对于给定的m,n,求出最大的邮资连续区间。例如,给定n=3,m=3,邮票面值分别为2, 3, 5,则最大的邮资连续区间为[2,13]。
2. 具体要求
Input
输入的第一行是一个正整数n,表示测试例个数。接下来几行是n个测试例的数据,每个测试例的数据由两行组成,其中第一行含两个正整数n和m (1<=n, m<=10),表示有n种邮票,每封信最多贴m张邮票;第二行含n个正整数,表示n种邮票的面值。同一行整数之间用一个空格隔开。
Output
对于每个测试例输出一行,含两个整数,依次为最大的邮资连续区间的下界和上界,当存在多个最大的邮资连续区间时,输出下界值最小的一个。同一行两个整数之间用一个空格隔开。
3. 测试数据
Sample Input
2
3 3
2 3 5
4 5
1 4 12 21
Sample Output
2 13
1 71
4.设计与实现的提示
(1) 可以有很多不同思路,其中一种为,从考虑某个面值能否被取到入手
5. 扩展内容
// fenjie.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
struct SE
{
int start;
int end;
};
bool zuhe(int num[],int lenth, int index, int n, int count)
{
bool bFenjie = false;
if(n == 0)
{
return true;
}
if(n < 0)
{
return false;
}
for(int i=index; i<lenth && count!=0; i++)
{
bFenjie = zuhe(num, lenth, i, n-num[i], count-1);
if(bFenjie)
{
return true;
}
}
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
int longdistance = 0;
int num[3] = {2,3,5};
int count = 3;
int max = num[2]*count;
int min = num[0];
vector<SE> vt;
vector<SE> rt;
while(min <= max)
{
bool bFenjie = false;
bFenjie = zuhe(num, 3, 0, min, count);
if(bFenjie == true)
{
if(vt.empty())
{
SE se = {min, min};
vt.push_back(se);
}
(vt.end()-1)->end = min;
}
else
{
if(!vt.empty())
{
rt.push_back(vt.front());
}
vt.clear();
}
min++;
}
if(!vt.empty())
{
rt.push_back(vt.front());
}
for(vector<SE>::iterator it = rt.begin(); it != rt.end(); it++)
{
if( (it->end - it->start) > longdistance )
{
longdistance = it->end - it->start;
min = it->start;
max = it->end;
}
}
cout<<"longist distance is "<<longdistance<<endl;
cout<<"start: "<<min<<" end: "<<max<<endl;
cin.get();
return 0;
}