暑假第二场积分赛-----————————D题

zz又碰到了一个数学小问题,定义一个函数P(x) ,例如:P(123) = 1! 2! 3!
求在满足P(z) = P(x)的情况下,最大化z (x z; z 6= 1; z 6= 0)
输入
第1行输入T(1 T 20)组数据
第2行输入n(1 n 100),表示x数字的长度
第3行输入正整数x
输出
输出最大的z(数据保证x内含大于1的数,所以z必定有解)
输入样例
2
4
1234
3
555
输出

33222
555
HINT
第一个样例f(1234) = 1! 2! 3! 4! = 288 = f(33222)

题意分析:就是把给你的数字的每一个能拆分成其他阶乘的数字都拆分,另外因为长度为100,所以用字符串来接收

比如  9=7!*8*9    而8=2*4,9=3*3;2=1*2,所以9!=7!*3!*3!*2!;

依次类推可得出所有的可拆分的数字的拆分形式(注意1要舍去,因为如果不舍弃1的话,可以在原x后无限加1,这样乘积不变,而大小扩大

代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#include<string>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
    {
        vector<int> ac;
        ac.clear();
        string s;
        int n;
        cin>>n>>s;
        int x;
        for(int i=0;i<n;i++)
        {
            x=s[i]-'0';
            if(x==9) {ac.push_back(7);ac.push_back(3);ac.push_back(3);ac.push_back(2);}
            else if(x==8)
            {
                ac.push_back(7);ac.push_back(2);
                ac.push_back(2);ac.push_back(2);
            }
           else  if(x==6) {
                ac.push_back(5);ac.push_back(3);
            }
           else  if(x==4){
                ac.push_back(3);ac.push_back(2);ac.push_back(2);
            }
            else if(x!=1&&x!=0) ac.push_back(x);
        }
        sort(ac.begin(),ac.end(),greater<int>());
        for(int i=0;i<ac.size();i++) cout<<ac[i];
       // puts("");
       cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41670466/article/details/81428345
今日推荐