美文网首页
Educational Codeforces Round 36

Educational Codeforces Round 36

作者: _弓长_大人 | 来源:发表于2018-09-25 12:46 被阅读2次

C题意: 给a,b 两个数 求 将a的数字换位置 组成的 最大的 小于 b 的数。
题解 思路 1: 将 a的每一位数存在 数组中排序,从大开始从高位开始放数,如果 加上它小于等于b,而且剩下的数 组成的最小值 加上也 小于b 则选择这个数,再选择下面一个数。贪心求得最值,我之前是这种

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int num[10];
ll ten[20];
ll a,b;
void dfs(int c,ll co)
{

    if(c<0)
    {
        cout<<co;
        exit(0);
    }
    else
    {
        for(int i=9;i>=0;i--)
        {
            if(num[i]>0&&ten[c]*i+co<=b)
            {
                    num[i]--;
                    dfs(c-1,co+ten[c]*i);
                    num[i]++;
            }
        }
    }
}
int main()
      {
        int cn=0;

        cin>>a>>b;
        ll x=a;
        ten[0]=1;for(int i=1;i<19;i++)ten[i]=ten[i-1]*10;
        while(x)
        {
            num[x%10]++;
            x/=10;
            cn++;
        }
        dfs(cn-1,0);
        return 0; }


相关文章

网友评论

      本文标题:Educational Codeforces Round 36

      本文链接:https://www.haomeiwen.com/subject/kevyoxtx.html