当前位置: 代码迷 >> 综合 >> 2020牛客多校 The Crime-solving Plan of Groundhog
  详细解决方案

2020牛客多校 The Crime-solving Plan of Groundhog

热度:75   发布时间:2024-02-08 11:22:33.0
题目描述

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 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.

译文:
今天,ZLZX有一个神秘的情况:Orange把挂在宿舍里的羽绒服弄丢了。在大家的期待下,土拨鼠侦探拿起他的小勺子,开始了破案的旅程。
土拨鼠深入调查每层最北的神秘房间后,发现了n个神秘数字。只要这些数字所传达的线索被破译,他就能揭示事情的真相。破译方法是:用这些数字生成两个不带前导零的正整数,并最小化这两个正整数的乘积是最终的线索。
然后土拨鼠想知道:最小的产品是什么?
当他继续在新楼西边的房间里调查时,他向你提出了这个问题。
简写:给定0到9之间的n个数,将它们拼成两个正整数,不带前导零,使乘积最小。

输入描述

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

输出描述

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

样例1
输入:

1
4
1 2 2 1

输出:

1223
10

样例2
输入:

2
5
1 3 2 1 2
3
1 1 0

输出:

1223
10

备注:

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int n;
int main()
{int n;scanf("%d",&n);while(n--){//输入数据int num;scanf("%d",&num);int a[num];for(int i = 0;i < num;i++){scanf("%d",&a[i]);}//对数组进行处理sort(a,a+num);for(int i = 0;i < num;i++){if(a[0] == 0){if(a[i] != 0){swap(a[i],a[0]);swap(a[1],a[i+1]);break;}}}//模拟乘积,高精度int k = 0;for(int i = num - 1;i > 0;i--){k = a[0] * a[i] + k;a[i] = k % 10;k /= 10;}//输出if(k){printf("%d",k);}for(int i = 1;i < num;i++){printf("%d",a[i]);}printf("\n");}
}

输入完数据后对数组进行排序。当出现前导0时将第一个数与第一个非0数互换位置,第二个数与第二个非0数互换位置。
这样第一个数就是第一个乘数,第二个数及之后的各数组成第二个乘数。因为数字较大,所以用高精度的写法来写一个模拟乘积,之后输出计算出的结果就行了。