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.(0≤ai≤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
 
n个数字组成一个数,问能够组成的最大的奇数是多少?不能有前导0
直接模拟:1,如果全是偶数直接输出-1
       2,从大到小排序,最低位为奇数直接输出,最低位为偶数的话往前挪,找到第一个奇数,注意一下前导0的情况即可。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
int cmp(int a,int b)
{
return a>b;
}
int main()
{
int n,a[];
char c[];
while(scanf("%d",&n)!=EOF)
{
bool flag = false;
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
if(a[i]%==) flag = true;
}
if(!flag) printf("-1\n");
else
{
int res[];
sort(a+,a+n+,cmp);
if(a[n]%==)
{
for(int i=; i<=n; i++)
{
res[i] = a[i];
}
}
else
{
int t = n;
for(int i=n; i>=; i--)
{
if(a[i]%==)
{
t = a[i];
a[i] = -;
break;
}
}
int cnt=;
for(int i=; i<=n; i++)
{
if(a[i]==-) continue;
res[cnt++] = a[i];
}
res[cnt] = t;
}
if(res[]==) printf("-1\n");
else
{
for(int i=; i<=n; i++)
{
printf("%d",res[i]);
}
printf("\n");
}
}
}
return ;
}

hdu 5055(模拟)的更多相关文章

  1. hdu 5055

    http://acm.hdu.edu.cn/showproblem.php?pid=5055 n个digit能组合出的最大无前导0奇数 无聊的模拟 #include <cstdio> #i ...

  2. HDU 5055 Bob and math problem(简单贪心)

    http://acm.hdu.edu.cn/showproblem.php?pid=5055 题目大意: 给你N位数,每位数是0~9之间.你把这N位数构成一个整数. 要求: 1.必须是奇数 2.整数的 ...

  3. HDU 5055 Bob and math problem(结构体)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5055 Problem Description Recently, Bob has been think ...

  4. hdu 4891 模拟水题

    http://acm.hdu.edu.cn/showproblem.php?pid=4891 给出一个文本,问说有多少种理解方式. 1. $$中间的,(s1+1) * (s2+1) * ...*(sn ...

  5. hdu 5012 模拟+bfs

    http://acm.hdu.edu.cn/showproblem.php?pid=5012 模拟出骰子四种反转方式,bfs,最多不会走超过6步 #include <cstdio> #in ...

  6. hdu 5055(坑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5055 Bob and math problem Time Limit: 2000/1000 MS ( ...

  7. hdu 4669 模拟

    思路: 主要就是模拟这些操作,用链表果断超时.改用堆栈模拟就过了 #include<map> #include<set> #include<stack> #incl ...

  8. 2013杭州网络赛C题HDU 4640(模拟)

    The Donkey of Gui Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. HDU/5499/模拟

    题目链接 模拟题,直接看代码. £:分数的计算方法,要用double; #include <set> #include <map> #include <cmath> ...

随机推荐

  1. oracle redo 重做日志文件

    以下易容翻译自oracle dba官方文档,不足之处还望指出. 管理重做日志文件 学习目标:1.解释重做日志文件的目的2.描述重做日志文件的结构3.学会控制日志切换与检查点4.多元化管理重做日志文件5 ...

  2. 机顶盒demux的工作原理

    在机顶盒中demux部分相对来说是比较复杂的部分,对于机顶盒软件开发的新手来说通常在这里会遇到一些困难,今天特意研究了一下驱动层代码,有一点自己的理解,因此写下来记录一下学习过程. 机顶盒中数据是如何 ...

  3. BFS:HDU-1242-Rescue(带守卫的迷宫问题)(优先队列)

    解题心得: 1.读清楚题意,本题的题意是有多个'r'(起点),多个r多个bfs比较最短的时间即可,但是hdoj的数据比较水,直接一个起点就行了,迷宫里有多个守卫,如果在路途中遇到守卫会多花费一个时间点 ...

  4. hadoop: Shuffle过程详解 (转载)

    原文地址:http://langyu.iteye.com/blog/992916 另一篇博文:http://www.cnblogs.com/gwgyk/p/3997849.html Shuffle过程 ...

  5. SQL中的函数用法

    一.coalesce COALESCE (expression_1, expression_2, ...,expression_n)依次参考各参数表达式,遇到非null值即停止并返回该值.如果所有的表 ...

  6. nable to execute dex: Multiple dex files define Lcom/chinaCEB/cebActivity/R

    用proguaid 只混淆Android项目的src下的包的话,如果出现了上面的问题: nable to execute dex: Multiple dex files define Lcom/chi ...

  7. Sentry 错误监控

    错误监控:https://sentry.io 支持语言或平台: 

  8. 【Linked List Cycle II】cpp

    题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

  9. windows下SecureCRT无法使用Backspace(删除键)和上下左右键

    MongoDB Shell中退格键使用的问题. 利用SecureCRT工具访问linux的时候,在使用MongoDB的交互式shell的时候,退格键(Backspace)无法使用,导致无法修改输入的字 ...

  10. Python 拓展之特殊函数(lambda 函数,map 函数,filter 函数,reduce 函数)

    写在之前 今天给大家介绍几个比较特殊的函数,他们具有函数式编程的特点,有人将它们视为 Python 可进行 "函数式编程" 的见证,至于什么是函数式编程,不是本篇文章的重点,感兴趣 ...