uva 165 - Stamps

就是连续邮资问题。。

回朔法 sum 是求出每次最大连续的值

 backtrace 是枚举出每次的加入的值

 

 

题目:

Stamps 

The government of Nova Mareterrania requires that various legal documents have stamps attached to them so that the government can derive revenue from them. In terms of recent legislation, each class of document is limited in the number of stamps that may be attached to it. The government wishes to know how many different stamps, and of what values, they need to print to allow the widest choice of values to be made up under these conditions. Stamps are always valued in units of $1.

 

This has been analysed by government mathematicians who have derived a formula for n(h,k), where h is the number of stamps that may be attached to a document, k is the number of denominations of stamps available, and n is the largest attainable value in a continuous sequence starting from $1. For instance, if h=3, k=2 and the denominations are $1 and $4, we can make all the values from $1 to $6 (as well as $8, $9 and $12). However with the same values of h and k, but using $1 and $3 stamps we can make all the values from $1 to $7 (as well as $9). This is maximal, so n(3,2) = 7.

 

Unfortunately the formula relating n(h,k) to h, k and the values of the stamps has been lost--it was published in one of the government reports but no-one can remember which one, and of the three researchers who started to search for the formula, two died of boredom and the third took a job as a lighthouse keeper because it provided more social stimulation.

The task has now been passed on to you. You doubt the existence of a formula in the first place so you decide to write a program that, for given values of h and k, will determine an optimum set of stamps and the value of n(h,k).

 

Input

Input will consist of several lines, each containing a value for h and k. The file will be terminated by two zeroes (0 0). For technical reasons the sum of h and k is limited to 9. (The President lost his little finger in a shooting accident and cannot count past 9).

 

Output

Output will consist of a line for each value of h and k consisting of the k stamp values in ascending order right justified in fields 3 characters wide, followed by a space and an arrow (->) and the value of n(h,k) right justified in a field 3 characters wide.

 

Sample input

 

3 2
0 0

 

Sample output

 

  1  3 ->  7



代码:

 1 #include <iostream>
 2 #include <memory.h>
 3 #include <cstdio>
 4 #include <iomanip>
 5 using namespace std;
 6 
 7 int h,k;
 8 int kind[20];
 9 int range[20];
10 int vis[1000];
11 int finalans[20];
12 int MAX = 0;
13 void sum(int n,int cur, int total)
14 {
15     if(cur == h)
16     {
17         vis[total]=1;
18         return;
19     }
20     vis[total]=1;
21     for(int i=0;i<=n;i++)
22     {
23         sum(n,cur+1,total+kind[i]);
24     }
25 }
26 void backtrace(int cur,int num)
27 {
28     if(cur == k+1)
29     {
30         if(num-1>MAX)
31         {
32             MAX=num-1;
33             memcpy(finalans,kind,sizeof(finalans));
34         }
35 
36     }
37     else
38     {
39         for(int i=kind[cur-1]+1;i<=num;i++)
40         {
41             kind[cur] = i;
42             memset(vis,0,sizeof(vis));
43             sum(cur,0,0);
44             int j = kind[cur-1]+1;
45             while(vis[j++])
46             {
47 
48             }
49 
50             backtrace(cur+1,j-1);
51         }
52 
53     }
54 
55 
56 
57 
58 }
59 int main()
60 {
61 
62     while(cin>>h>>k && h && k)
63     {
64         kind[0]=0;
65         kind[1]=1;
66 
67 
68         backtrace(2,h+1);
69 
70         for(int i=1;i<=k;i++)
71         {
72             cout<<setw(3)<<finalans[i];
73         }
74         cout<<" ->";
75         cout<<setw(3)<<MAX<<endl;
76 
77         MAX=0;
78         memset(kind,0,sizeof(kind));
79         memset(vis,0,sizeof(vis));
80     }
81 
82 
83     return 0;
84 }

 

转载于:https://www.cnblogs.com/doubleshik/p/3484460.html

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值