The maximum number of matchstick can be composed of

The maximum number of matchstick can be composed of

2020 Spring trick Baidu pen questions

topic:

Matchstick input the number n, the number of digits m can be selected, and the selected list of numbers can be
input case (case I forget the original title, Xia Xie)
20 is. 4
. 5. 6. 7. 8
outputs can be composed greatest integer

Solution: use dynamic programming, the code github link , if the error also hope noted Novice thanking you in advance

#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;
int number[10] = { 0,1,2,3,4,5,6,7,8,9 };
int expend[10] = { 0,2,5,5,4,5,6,3,7,6 };
int main() {
	int n, m;
	scanf("%d", &n);
	scanf("%d", &m);
	vector<bool> canuse(10, false);
	vector<int> dp(n+1,0);
	for (int i = 0; i<m; i++) {
		int temp;
		scanf("%d", &temp);
		canuse[temp] = true;
	}
	dp[0]=0;
	for(int i=1;i<=n;i++){
		int choice=0;
		int maxvalue=0;
		for(int j=1;j<=9;j++){
			if(canuse[j]) {
				if(i-expend[j]<0) continue;
				if(dp[i-expend[j]]==-1) continue;
				if(dp[i-expend[j]]>=maxvalue){
					choice=j;
					maxvalue=dp[i-expend[j]];
				}
			}
		}
		if(choice==0) dp[i]=-1;
		else{
			dp[i]=maxvalue*10+choice;
		}
		//printf("%d\n",dp[i]);
	}
	printf("%d",dp[n]);
	return 0;
}

2020 March 14

Published 15 original articles · won praise 5 · Views 3673

Guess you like

Origin blog.csdn.net/qq_28632981/article/details/104871619
Recommended