2020牛客暑期多校训练营(第九场)I .The Crime-solving Plan of Groundhog
题目链接
题目描述
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 {n}n 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 is the smallest product?
As he continued to investigate in the room west of the new building, he gave you the question.
Concise meaning:Given n numbers between 0 and 9, 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
思维题,很容易想到选一个最小的数字出来,再把剩下的数字拼成一个最小的数,然后相乘即可,注意两个数必须要是正数,且没有前导 ,可以对数组先进行排序,找到第一个不是 的位置,然后替换两次就能得到最小的数了,举个例子
0000123->替换第1个和第5个1000023->替换第2个和第6个1200003->1|200003
对第
个到第
个进行模拟乘法即可~
1.C++代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int n,t,a[N];
int main()
{scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=0;i<n;i++) scanf("%d",&a[i]);sort(a,a+n);int pos=0;while(!a[pos]) pos++;swap(a[0],a[pos]);swap(a[1],a[pos+1]);for(int i=1;i<n;i++) a[i]*=a[0];for(int i=n-1;i>1;i--) a[i-1]+=a[i]/10,a[i]%=10;for(int i=1;i<n;i++) printf("%d",a[i]);printf("\n");}return 0;
}
2.python代码如下:
t=int(input())
for i in range(t):n=int(input())a=input().split()a.sort()i=0while a[i]=='0':i+=1x=a[i]y=a[i+1]+'0'*i+"".join(a[i+2:])print(int(x)*int(y))