Algorithm training stamp

/*Problem description
Given an envelope, there are N (1 ≤ N ≤ 100) positions where stamps can be pasted, and only one stamp can be pasted at each position.
We now have m (m < = 100) stamps with different postage. The face value is X1,X2 . Xm points (Xi is an integer, 1 ≤ Xi ≤ 255), each of which has N pieces.
Obviously, the minimum postage that can be posted on an envelope is min(X1, X2,...) , XM), the maximum value is N*max(X1, X2 (Xm).
The postage value obtained by all pasting methods can form a set (there are no duplicate values in the set). It is required to find out whether there is a continuous postage sequence from 1 to a certain value in the set, and output the maximum value of the sequence.
For example, N=4, M=2, and the face value is 4 points, 1 point, respectively, thus forming a sequence of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 16,
The continuous postage sequence starting from 1 is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, so the maximum value of the continuous postage sequence is 10 points.
Input format
The first line: the maximum number of stamps allowed to be pasted N; the second line: the number of stamps M; the third line: M numbers separated by spaces, indicating the face value Xi of stamps.
Note: Xi sequences are not necessarily ordered in size!
Output format
The maximum value MAX of the consecutive postage sequence starting from 1. If there is no sequence starting from 1 minute (i.e. there is no 1 minute stamp in the input stamp),
Output 0.
sample input
Example 1:
4
2
4 1
Example two:
10
5
2 4 6 8 10
sample output
Example 1:
10
Example two:
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]);
	}
}

 

Posted by Tyree on Fri, 03 Jan 2020 18:22:59 -0800