hdu4726 Kia's Calculation 贪心

 给两个数,进行不进为加法(比如9+9=8),可以调换每个数中数字的位置,但不能有前导零,求A+B得最大值。

         题目要求不能有前导零,那么我们就先找出来一个不使用0可以得到的最大的数,把他输出后剩下的就可以正常求了,从9到0枚举每个数,然后根据给的两个数中每个数出现的次数,求出可以构造出几个i,从大到小扫一遍即可。

           

/*=============================================================================
#  Author:Erich
#  FileName:
=============================================================================*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#define lson id<<1,l,m
#define rson id<<1|1,m+1,r

using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=1ll<<60;
const double PI=acos(-1.0);
int n,m;
int a[11],b[11];
char s[1000010];
int num[15];
int main()
{
	//freopen("in.txt","r",stdin);
	int tt;
	scanf("%d",&tt);
	int l;
	int cnt=0;
	while(tt--)
	{
		printf("Case #%d: ",++cnt);
		memset(a,0,sizeof a);
		memset(b,0,sizeof b);
		scanf("%s",s);
		l=strlen(s);
		for (int i=0; i<l; i++)
		{
			a[s[i]-'0']++;
		}
		scanf("%s",s);
		for (int i=0; i<l; i++)
		{
			b[s[i]-'0']++;
		}
		memset(num,0,sizeof num);
		bool has=false;
		for (int i=9; i>=1; i--)
		{
			for (int j=1; j<=9; j++)
			{
				int k=((i-j)+10)%10;
				if (k==0) continue;
				m=min(a[j],b[k]);
				if (m)
				{
					m=1;
					a[j]--; b[k]--;
					printf("%d",i);
					has=true; break;
				}
				if (has) break;
			}
			if (has) break;
		}
		if (!has)
		{
			printf("0");
		}
		else 
		{

		
		for (int i=9; i>=0; i--)
		{
			for (int j=0; j<=9; j++)
			{
				int k=((i-j)+10)%10;
				m=min(a[j],b[k]);
				if (m)
				{
					a[j]-=m;
					b[k]-=m;
					while(m--) printf("%d",i);
				}
			}
		}
		}
		puts("");

	}


	return 0;
}

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章