传送门
思路:
- 题意: 给出n个数,将其拼接成两个没有前导零的正整数,使得他们的乘积最小。
- 官方题解:
- 我想的思路个题解一样,但是我在补题的时候出现了一个小问题(代码中已注释),导致我一直改其他地方一直wa,呜呜呜~
代码实现:
#include<bits/stdc++.h>
//#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
{
1, 0}, {
-1, 0}, {
0, 1}, {
0, -1}};
using namespace std;
const int inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;int t, n, a[N], b;
vector<int> A, C;int cmp(int a, int b){
return a > b;
}vector<int> mul(vector<int> &A, int b)
{
vector<int> C;int t = 0;for(int i = 0; i < A.size(); i ++){
t += A[i] * b;C.push_back(t % 10);t /= 10;} //高精度乘法板子的小瑕疵,这里的t不能放在循环的判断语句里写成(i < A.size() || t)while(t) C.push_back(t % 10), t /= 10;while(!C.back() && C.size()>1) C.pop_back();return C;
}signed main()
{
IOS;cin >> t;while(t --){
cin >> n;int minn = inf, maxx = 0, cnt0 = 0;for(int i = 0; i < n; i ++){
cin >> a[i];if(!a[i]) cnt0 ++;else minn = min(minn, a[i]);maxx = max(maxx, a[i]);}b = minn; //取出最小的正整数sort(a, a+n, cmp);int pos = -1; //记录最小的首位数字在a[]中的位置for(int i = n-1; ~i; i --) if(a[i] == minn) {
pos = i-1;break;}
// cout << "pos: " << pos << endl;if(pos == -1){
cout << 0 << endl;continue;}bool flag = 0; A.clear();for(int i = 0; i < pos; i ++) A.push_back(a[i]);while(cnt0 --) A.push_back(0); A.push_back(a[pos]);// cout << "A: ";
// for(int i = 0; i < A.size(); i ++) cout << A[i];
// cout << endl;
// cout << "b: " << b << endl;if(b > 1) C = mul(A, b);else C = A;for(int i = C.size()-1; ~i; i --) cout << C[i];cout << endl;}return 0;
}