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

Bob and math problem

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

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 $a_1, a_2, a_3, \cdots, a_n. ( 0 \leq a_i \leq 9)$.
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
Source
BestCoder Round #11 (Div. 2)
Recommend
heyang   |   We have carefully selected several similar problems for you:  5057 5056 5054 5053 5052 

思路:这题有点略坑~思路挺简单,但是细心才能AC,

直接将n个数排序,然后找最小的奇数移出即可;

PS:(1)要注意n==1的情况

(2)You need to use this N Digits to constitute an Integer.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cmath>
const int INF=99999999;
#include <algorithm>
using namespace std;int a[110];
bool cmp(int a,int b)
{return a>b;
}
int main()
{int n;while(cin>>n){for(int i=1;i<=n;i++){cin>>a[i];}if(n==1){if(a[1]&1)cout<<a[1]<<endl;elsecout<<-1<<endl;continue;}sort(a+1,a+1+n,cmp);int flag=INF;for(int i=n;i>=1;i--){if(a[i]&1){flag=i;break;}}if(flag==INF){cout<<-1<<endl;continue;}if(flag==1&&a[2]==0){cout<<-1<<endl;continue;}for(int i=1;i<=n;i++){if(i!=flag)cout<<a[i];}cout<<a[flag]<<endl;}return 0;
}

hdu 5055(坑)相关推荐

  1. hdu 5055(贪心)

    题意:给你N个数,每个数的取值范围为[0,9],求这N个数组成的最大奇数,且该奇数不能有前导零. 解题思路:将偶数和奇数分开,然后分别按从小到大排序,将最小的奇数先拿出来,把剩余的数从大到小排好,最后 ...

  2. HDU 5055 Bob and math problem(构造)

    无法输出的情况稍微难处理 其他的只要贪心构造一下就好了 #include<iostream> #include<cstdio> #include<set> #inc ...

  3. 程序员用学位证吗_如何成为没有学位或新兵训练的开发人员

    程序员用学位证吗 Preface: This post is geared towards people interested in being self-taught because boot-ca ...

  4. HDU 2549 壮志难酬 (水题,但有个小坑!)

    壮志难酬 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. hdu 4738 无向图缩点断桥 // 细节坑题

    Caocao's Bridges 题意:给个无向图,求出边权最小的桥. 一看,直接缩点,若无桥,输出-1,有桥,遍历下边,更新最小..分分钟搞定,以为IA的..一交wa... 坑点:1:若原图不连通, ...

  6. HDU 2092 算菜价(结束条件,坑呀!)

    算菜价 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  7. HDU 4279 占坑先。。。

    RT... 转载于:https://www.cnblogs.com/fakeAcmer/archive/2012/10/18/2729981.html

  8. hdu 2879【留坑】

    看了题解,说的都不太准确,可能是积性函数是为莫比乌斯准备的吧,莫比乌斯真的难 #include <bits/stdc++.h> using namespace std; typedef l ...

  9. 【HDU - 5468】Puzzled Elena(容斥原理,dfs序,数学,素因子分解,有坑)

    题干: Problem Description Since both Stefan and Damon fell in love with Elena, and it was really diffi ...

最新文章

  1. tomcat苹果版安装步骤_Mac系统安装和配置tomcat步骤详解
  2. 如果成为一名高级安卓开发_什么是高级开发人员,我如何成为一名开发人员?
  3. Java 多线程 之 suspend挂起 线程实例
  4. 腾讯offer是什么样子_记一次腾讯社招前端面试(已拿到offer入职)
  5. flex 3 使用手册
  6. Linux高效数据统计命令wc
  7. 【电影】美丽心灵的永恒阳光
  8. Spotify模式并非“敏捷涅磐”
  9. 字节面试官:如何实现Ajax并发请求控制
  10. python基本语法:序列
  11. 职称计算机技巧集锦,2014职称计算机考试《Excel》使用技巧集锦(4)
  12. signature=fa342ee2b7c3e3e9cba3f194df2d59ca,Date of Signature
  13. java 求向量的均值,标准数组——向量
  14. 微信小程序在地图上标点 markers 画圈显示范围 circles
  15. 命令查询职责分离模式 CQRS Command Query Responsibility Segregation
  16. 炼油化工常用英文缩写
  17. 数字科技陪伴企业成长|突破封锁,庚顿数据助力中国名牌全球瞩目
  18. star测试软件,PSTAR准备
  19. CDATA 数据处理
  20. Linux下Socket编程之TCP原理

热门文章

  1. 使用vue3实现数据大屏展示
  2. 小程序视频不显示进度条,且不能滑屏快进
  3. glob.glob() 函数
  4. POI中设置Excel单元格格式样式(居中,字体,边框,背景色、列宽、合并单元格等)
  5. 4.4 赋值运算符,4.5 递增和递减运算符
  6. 数控自动编程软件比你想象的更方便!
  7. 想问题思路总是不清晰,这个好用的脑图工具推荐给你
  8. Unity射击游戏发射子弹的方法和提升流畅性的小技巧
  9. 将SMILES转化为结构式的工具--SMILESDrawer介绍
  10. 巧用计算机课件,如何用电脑录制ppt讲解视频?