2020牛客暑期多校训练营(第九场)I .The Crime-solving Plan of Groundhog

侦探Groundhog面对神秘案件,需要从一组数字中找出最小的两个正整数的乘积,以揭示案件真相。本篇介绍如何通过算法解决这一问题,包括数据排序、寻找非零数字等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 T T T,the number of test cases.
For each set of data:
Each test case begins with a single integer n n n, the count of numbers.
The next line are n n n 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

思维题,很容易想到选一个最小的数字出来,再把剩下的数字拼成一个最小的数,然后相乘即可,注意两个数必须要是正数,且没有前导 0 0 0,可以对数组先进行排序,找到第一个不是 0 0 0 的位置,然后替换两次就能得到最小的数了,举个例子

0000123->替换第1个和第5个1000023->替换第2个和第6个1200003->1|200003

对第 2 2 2 个到第 n n n 个进行模拟乘法即可~
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=0
    while a[i]=='0':
        i+=1
    x=a[i]
    y=a[i+1]+'0'*i+"".join(a[i+2:])
    print(int(x)*int(y))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺 崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值