解放1
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int M=1e5+20;
char a[M];
int n;
int num[10]={6,2,5,5,4,5,6,3,7,6};//组成数字i需要多少根木棒
bool check(long long cnt,int i)
{
return cnt>=(n-1-i)*2&&(n-1-i)*7>=cnt;//必选保证剩下的cnt 能放完
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
long long cnt=0;
scanf("%s",a);
for(int i=0;i<n;i++)
{
cnt+=num[a[i]-'0'];//统计总共有多少根木棒
}
for(int i=0;i<n;i++)
{
for(int k=9;k>=0;k--)//数字尽量大 高位尽量大
{
if(check(cnt-num[k],i))//能否放该数字
{
cnt-=num[k];
a[i]=k+'0';
break;
}
}
}
printf("%s\n",a);
}
return 0;
}
解放2
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int M=110000;
char a[M];
int n;
struct dat{
int stick;
int many;
}s[10]={{0,4},{1,0},{2,3},{3,3},{4,2},{5,3},{6,4},{7,1},{8,5},{9,4}};
bool cmp(dat a,dat b)
{
if(a.many==b.many)
return a.stick>b.stick;
return a.many>b.many;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
int num[10]={4,0,3,3,2,3,4,1,5,4};//组成数字i需要多少根木棒
long long cnt=0;
scanf("%s",a);
for(int i=0;i<n;i++)
{
cnt+=num[a[i]-'0'];//统计总共有多少根木棒
a[i]='1';
}
int k=9;
for(int i=0;i<n;i++)//尽量大
{
while(cnt<num[k])
{
k--;
}
cnt-=num[k];
a[i]=k+'0';
}
if(cnt)
{
k=0;
sort(s,s+10,cmp);
for(int i=n-1;i>=0;i--)//数字已经最大化 任何改变都会使数值变低
//所以从低位开始尽快把stick用完
{
cnt+=num[a[i]-'0'];
while(cnt<s[k].many&&k<10)
{
k++;
}
cnt-=s[k].many;
a[i]=s[k].stick+'0';
if(cnt==0)// 用完就退出
break;
}
}
// cout<<"@"<<cnt<<endl;
printf("%s\n",a);
}
return 0;
}