算法训练 邮票

博客围绕邮票贴法问题展开,给定信封有N个贴邮票位置,有M种不同邮资邮票,每种有N张。需根据输入的N、M及邮票面值,求出所有贴法形成的邮资集合中,从1开始的连续邮资序列的最大值,还给出了输入输出格式及样例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*问题描述
给定一个信封,有N(1≤N≤100)个位置可以贴邮票,每个位置只能贴一张邮票。
我们现在有M(M<=100)种不同邮资的邮票,面值为X1,X2….Xm分(Xi是整数,1≤Xi≤255),每种都有N张。
显然,信封上能贴的邮资最小值是min(X1, X2, …, Xm),最大值是 N*max(X1, X2, …, Xm)。
由所有贴法得到的邮资值可形成一个集合(集合中没有重复数值),要求求出这个集合中是否存在从1到某个值的连续邮资序列,输出这个序列的最大值。
例如,N=4,M=2,面值分别为4分,1分,于是形成1,2,3,4,5,6,7,8,9,10,12,13,16的序列,
而从1开始的连续邮资序列为1,2,3,4,5,6,7,8,9,10,所以连续邮资序列的最大值为10分。
输入格式
第一行:最多允许粘贴的邮票张数N;第二行:邮票种数M;第三行:空格隔开的M个数字,表示邮票的面值Xi。
注意:Xi序列不一定是大小有序的!
输出格式
从1开始的连续邮资序列的最大值MAX。若不存在从1分开始的序列(即输入的邮票中没有1分面额的邮票),
则输出0.
样例输入
样例一:
4
2
4 1
样例二:
10
5
2 4 6 8 10
样例输出
样例一:
10
样例二:
0
20
10
1 14 101 116 144 168 178 228 242 247

*/

#include<stdio.h>  
void shuru( int [] , int );
void shuchu( int [], int );
int q_max( int [] , int );
void chushihua( int [] , int );
void jisuan(int [] , int [] , int , int ,int  );
void fr_yp( int [] , int [] , int , int  );
int main( void ) 
{  
	int n , m , max ; 
	scanf("%d%d" ,& n , & m ) ;
	int sz[m] ;  
	shuru( sz ,  m );
	max = q_max( sz , m ) ;
	int cf[ n * max + 1] ; 
	chushihua( cf , n * max + 1 );
	cf[0] = 1 ;
	jisuan( cf , sz , n , m , max );
    shuchu( cf , n * max + 1 );
	return 0;  
}  
void chushihua( int cf[] , int n)
{
	int i ; 
	for(i = 0 ; i < n ; i ++ )
	{
		cf[i] = 0 ; 
	}	
}
void jisuan(int cf[] , int sz[] , int n , int m , int max )
{
	int i ; 
	for( i = 0 ; i < n ; i ++ )
	{
		int  j ; 
		for( j = i * max ; j >= 0 ; j -- )
		{
			if( cf[j] == 1 )
			{
				fr_yp( cf , sz , m , j );
			}
		}
	}
}
void fr_yp( int cf[] , int sz[] , int m , int cs )
{
	int i ; 
	for( i = 0 ; i < m ; i ++)
	{
		cf[cs + sz[i]] = 1 ; 
	}
}
int q_max( int sz [] , int n)
{
	int i , max = 0 ; 
	for( i = 0 ; i < n ; i ++ )
	{
		if( sz[i] > sz[max])
		{
			max = i ; 
		}
	}
	return sz[max] ; 
}
void shuchu( int sz[] , int n  )
{
	int i ; 
	for(i = 0 ; i < n ; i ++ )
	{
		if( sz[i] == 0 )
		{
			printf("%d\n" , i - 1);
			return ; 
		}
	}
}

void shuru( int sz[] , int n)
{
	int i ;
	for( i = 0 ; i < n ; i ++ )
	{
		scanf("%d", & sz[i]);
	}
}

 

DESCRIPTION: 1.Analyze Problem A : sorted stamps array A={ai} ai: one stamp element in array n: array size, that is number of elements in array r: desired value of stamps F(n,r):expected result, minimum number of stamps for a given value r from n size array. S: selected stamps array S={si} 2.Choose Algorithm a.Greedy algorithm seems to be a good choice, try to solve it in O(n), i try divide array into subarry B={bi}, r should larger than every elemnt in B that is r>bi and suppose bk is the smallest element in B, so that r= bk%r, f(i,r)=(bk/r), F(n,r)=∑f(i,r). The main idea is to choose the last element who larger than desired value each time. However,it can not give us optimal solution in some condition, like A={8,5,4,1}, if r=10, this algoritm will give a solution F(n,r)=3, S={8,1,1},but the optimal solution should be F(n,r)=2, S={5,5}. b.Full search so the straight forwards algorithm is to search for every solution in A for desired value directly.However, it will always take O(n!) to go through every combination. c.Dynamic programming, at last, I decide to choose dynamic programming. analyze optimal structure, suppose in A={ai}, for a specific stamp ak,there will be two cases it is choosen so that f(i,r)=1+f(i,r-ak) , 1<=i<=k, r>=ak it is not valid so that f(i,r)=f(i-1,r) 3.Design Dynamic programming optimal structure: Compute-opt(r)= 1 + Compute-opt(r-ai) value: Compute-opt(r) = ∞ (r < 0) Compute-opt(r) = 0 (r = 0) Compute-opt(r) = 1+{Compute-opt(r-ai)} ( 1=<i<=n, r>ai>0 ) Complexity :O(nr) Memory cost:O(n+r) Compute in a bottom-up style to recursive every desired value and array. store value of Compute-opt in memory for future use, so that we can easily get value from those memory in future recursive call, and avoid compute again, that is if the array is not change, you can easily fetch result any desired value j (j < r, r is the value using for compute ). 2.For User totally, I design a small command line for this machine list below 1.Manual Operation 2.Self Auto Testing 3.Check Results q.Quit Manual Operation: when select this machine will turn to be manual mode, ask person to input stamps and desired value; Self Auto Testing:when select this machine will turn to be auto mode, show the test case already design in code after that machine will quit automatically. Check Results: only be visiable in Manual Operation, people can check desired value for the array input before, the desired value should be no more than first time input. Quit, clean all the memory and quit system.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值