题目描述

  Today, ZLZX has a mysterious case, Orange lost his down jacket hanging in his dorm room. Under the expectations of everyone, detective Groundhog took his small spoon of the artifact and started the journey to solve the case.
  After an in-depth investigation of the northernmost mysterious room on each floor, Groundhog discovered mysterious numbers. As long as the clues conveyed by these numbers are deciphered, he can reveal the truth of the matter. The deciphering method is, using these numbers to generate two positive integers without leading zeros, and minimizing the product of these two positive integers is the final clue.
  Then Groundhog wants to know what the smallest product is?
  As he continued to investigate in the room west of the new building, he gave you the question.
  Concise meaning, given numbers between and , use them to make two positive integers without leading zeros to minimize the product.

输入描述

The first line of input is a single integer , the number of test cases.
  For each set of data, each test case begins with a single integer , the count of numbers.
  The next line are numbers.

输出描述

  For each set of case, an integer is output, representing the smallest product.

示例1

输入

1
4
1 2 2 1

输出

122

示例2

输入

2
5
1 3 2 1 2
3
1 1 0

输出

1223
10

备注

  , , .
  There are at least two Numbers that are guaranteed not to be zero.
  The Numbers range between .

分析

  若不考虑前导零的限制,取 个数中的最小值, 为其余 个数组合起来的最小整数, 即为最小乘积。
  考虑前导零后, 个数中的最小正整数, 为其余 个数组合起来的最小正整数。具体方法为:将 个数从小到大排序,取最小的正整数为 ;在剩余 个数中,取最小正整数为 的最高位,然后向 的低位补零,最后向 的低位从小到大不正整数。最后模拟个位数乘多位数的乘法输出

代码

/******************************************************************
Copyright: 11D_Beyonder All Rights Reserved
Author: 11D_Beyonder
Problem ID: 2020牛客暑期多校训练营(第九场) Problem I
Date: 9/3/2020
Description: Simulation
*******************************************************************/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const int N=100146;
vector<short>ans;
short a[N];
short y[N],x;
int n;
int _;
int main(){
    for(cin>>_;_;_--){
        scanf("%d",&n);
        int i;
        for(i=1;i<=n;i++){
            scanf("%hd",a+i);
        }
        sort(a+1,a+1+n);
        int pos;//不为0的最小值
        for(pos=1;pos<=n;pos++){
            if(a[pos]){
                break;
            }
        }
        x=a[pos];
        int len=0;
        for(i=pos+1;i<=n;i++){
            y[++len]=a[i];
            if(i==pos+1){
                int num=pos-1;
                while(num--){
                    //补零
                    y[++len]=0;
                }
            }
        }
        // debug
        // cout<<x<<endl;
        // for(i=1;i<=len;i++){
        //     cout<<y[i];
        // }
        // cout<<endl;
        reverse(y+1,y+1+len);
        //debug
        // for(i=1;i<=len;i++){
        //     cout<<y[i];
        // }
        // cout<<endl;
        short carry=0;
        ans.clear();
        //模拟乘法
        for(i=1;i<=len;i++){
            short num=x*y[i]+carry;
            carry=num/10;
            ans.push_back(num%10);
        }
        if(carry){
            ans.push_back(carry);
        }
        reverse(ans.begin(),ans.end());
        for(i=0;i<ans.size();i++){
            printf("%hd",ans[i]);
        }
        putchar('\n');
    }
    return 0;
}