题意:
给一个数,把它改写为另一个数,要求这个数最大且每一位阶乘求的和==初始数每一位阶乘求和
输入
3
555
产量
555
纯思维题,画个表:(我也是看了题解才会的......)
2!= 2!
3!= 3!
4!= 3!2!2!
5!= 5!
6!= 5!3!
7!= 7!
8!= 7!2!2!2!
9!= 7!3!3!2!
代码如下:
#include<cstdio>
#include<functional>
#include<algorithm>
#include<string>
#include<string.h>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1e3;
int n;
char x[maxn];
string a[maxn];
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> x[i];
a[2] = "2";
a[3] = "3";
a[4] = "322";
a[5] = "5";
a[6] = "53";
a[7] = "7";
a[8] = "7222";
a[9] = "7332";
string s;
int t = 1;
for (int i = n; i >= 1; i--) {
if (x[i] == '2')s += a[2];
if (x[i] == '3')s += a[3];
if (x[i] == '4')s += a[4];
if (x[i] == '5')s += a[5];
if (x[i] == '6')s += a[6];
if (x[i] == '7')s += a[7];
if (x[i] == '8')s += a[8];
if (x[i] == '9')s += a[9];
}
sort(s.begin(), s.end(),greater<char>());
cout << s << endl;
return 0;
}
补:这里说一下string类的排序吧:
sort(s.begin(), s.end(),greater<char>()),这条语句可以对string中的元素进行从大到小排序,greater<char>函数的头文件是:<functional>