hdu 5055 Bob and math problem(模拟)

本文介绍了一种算法问题,即如何从给定的一组0到9的数字中构造出满足特定条件的最大奇数整数。具体条件包括:该整数必须为奇数、不能有前导零,并且是在所有可能组合中数值最大的。文章通过示例说明了问题,并提供了一段C++代码实现,展示了如何筛选和排序数字以获得所需结果。

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

Bob and math problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1481    Accepted Submission(s): 552


Problem Description
Recently, Bob has been thinking about a math problem.
There are N Digits, each digit is between 0 and 9. You need to use this N Digits to constitute an Integer.
This Integer needs to satisfy the following conditions:
  • 1. must be an odd Integer.
  • 2. there is no leading zero.
  • 3. find the biggest one which is satisfied 1, 2.

Example: 
There are three Digits: 0, 1, 3. It can constitute six number of Integers. Only "301", "103" is legal, while "130", "310", "013", "031" is illegal. The biggest one of odd Integer is "301".
 

Input
There are multiple test cases. Please process till EOF.
Each case starts with a line containing an integer N ( 1 <= N <= 100 ).
The second line contains N Digits which indicate the digit a1,a2,a3,,an.(0ai9).
 

Output
The output of each test case of a line. If you can constitute an Integer which is satisfied above conditions, please output the biggest one. Otherwise, output "-1" instead.
 

Sample Input
3 0 1 3 3 5 4 2 3 2 4 6
 

Sample Output
301 425 -1
题意:给你n个0~9的数字,问你这些数字组合成的数中满足1:为奇数 2:没有前导零的最大数

思路:直接模拟即可。  最后一位取最小的奇数,第一位取最大的非零数,中间的数就从大到小取了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 110
int a[N],b[N];
bool cmp(int c,int d)
{
    return c>d;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        int odd=111,v;
        for(int i=0; i<n; i++)
            if(a[i]&1&&a[i]<odd)
            {
                odd=a[i];
                v=i;
            }
        if(odd==111) printf("-1\n");
        else
        {
            b[n-1]=odd;
            a[v]=-1;
            int maxn=-1;
            for(int i=0; i<n; i++)
                if(a[i]>0&&a[i]>maxn)
                {
                    maxn=a[i];
                    v=i;
                }
            if(maxn==-1&&n>1)
            {
                printf("-1\n");
                continue;
            }
            if(n>1)
            {
                b[0]=maxn;
                a[v]=-1;
                sort(a,a+n,cmp);
                int cnt=1;
                for(int i=0; i<n; i++)
                {
                    if(a[i]==-1) continue;
                    b[cnt++]=a[i];
                }
            }
            for(int i=0;i<n;i++)
                printf("%d",b[i]);
            printf("\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值