MYF

HDU 5055 Bob and math problem

题目链接

HDU 5055

解题方法:构造

题目分析

题目大意

n个数字,让这n个数字构造出一个没有前导0的最大奇数,输出该奇数,如果无法构造,输出-1

解析

n个数字递减排序,然后挑出最后一个奇数放到最后一个位置,如果找不到该奇数则输出-1,此时需要判断前导0,如果是0则输出-1

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <deque>
#include <algorithm>
#define Memset(a,val) memset(a,val,sizeof(a))
#define PI acos(-1)
#define rt(n) (i == n ? '\n' : ' ')
#define hi printf("Hi----------\n")
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<<"Debug : ---"<<x<<"---"<<endl;
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long ll;
using namespace std;
const int maxn=100000+5;
const int mod=1000000007;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
bool cmp(int x,int y){
return x>y;
}
int main()
{
int n;
int a[105];
while (cin>>n) {
for (int i=1; i<=n; i++) {
cin>>a[i];
}
sort(a+1, a+1+n, cmp);
for (int i=n; i>=1; i--) {
if (a[i]%2==1) {
a[n+1]=a[i];
a[i]=-1;
break;
}
if (i==1) {
n=-1;
cout<<n;
}
}
bool flag=true;
for (int i=1; i<=n+1; i++) {
if (a[i]!=-1) {
if (flag) {
if (a[i]==0) {
cout<<-1;
break;
}
}
cout<<a[i];
flag=false;
}
}
cout<<endl;
}
}