HDOJ 5055 Bob and math problem

20 篇文章 0 订阅
11 篇文章 0 订阅

题意:给你n个数字,组合成为一个数,要求如下:1.该数尽可能的大。2.无前导零。3.奇数。

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5055

思路:贪心思想,从大到小进行排序,将最小的一个奇数与最后一位进行交换,判断前导零,符合条件即输出结果。

注意点:前导零的判断,主要的hack点。

以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
117560952014-09-29 14:07:12Accepted505515MS348K1186 BG++luminous11
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    ios::sync_with_stdio( false );
    int num[1005];
    int t;
    while ( cin >> t )
    {
        bool flag = false;
        for ( int i  = 0; i < t; i ++ )
        {
            cin >> num[i];
            if ( num[i] & 1 )
            {
                flag =true;
            }
        }
        if ( ! flag )//剪枝,判断是否存在奇数
        {
            cout << "-1" << endl;
            continue;
        }
        flag = false;
        int k = -1;
        sort ( num, num + t );
        for ( int i = 0; i < t; i ++ )
        {
            if ( num[i] & 1 )
            {
                k = i;
                break;
            }
        }
        if ( k != -1 && num[t - 2] != 0 )//判断前导零
        {
            for ( int i = t - 1; i >= 0; i -- )
            {
                if ( k == i )
                {
                    continue;
                }
                cout << num[i];
            }
            cout << num[k] << endl;
        }
        else
        {
            cout << -1 << endl;
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值