邮票问题

(一)   邮票问题(Problem Set 1746):

1.    问题描述

设有n种不同面值a1, a2,…, an的邮票,规定每封信最多贴m张邮票。对于给定的mn,求出最大的邮资连续区间。例如,给定n=3m=3,邮票面值分别为2, 3, 5,则最大的邮资连续区间为[213]

2.       具体要求

Input

输入的第一行是一个正整数n,表示测试例个数。接下来几行是n个测试例的数据,每个测试例的数据由两行组成,其中第一行含两个正整数nm (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;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值