2020牛客多校 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 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数互换位置。
这样第一个数就是第一个乘数,第二个数及之后的各数组成第二个乘数。因为数字较大,所以用高精度的写法来写一个模拟乘积,之后输出计算出的结果就行了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值