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. python正则表达式入门篇

    文章来源于:https://www.cnblogs.com/chuxiuhong/p/5885073.html Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. ...

  2. Entrez Direct

    安装 cd ~/bin/bashperl -MNet::FTP -e \'$ftp = new Net::FTP("ftp.ncbi.nlm.nih.gov", Passive = ...

  3. notification 使用的基本方法

    当某个应用程序希望向用户发出一些提示信息,而应用程序又不在前台,可以借助Notification来实现.发出一条通知后,手机最上方额通知栏会显示一个图标,下来状态栏以后可以看到详细内容. 一.通知的基 ...

  4. 信号量和互斥量C语言示例理解线程同步

    Table of Contents 1. 线程同步 1.1. 用信号量进行同步 1.2. 用互斥量进行同步 2. 参考资料 线程同步 了解线程信号量的基础知识,对深入理解python的线程会大有帮助. ...

  5. [原]sencha touch之布局

    今天记录一下关于sencha touch中的几种布局,其实很简单的,还是直接上代码,一目了然 1:box布局,其实就是vbox和hbox,说白了一个是横着摆放,一个是竖着摆放 Ext.applicat ...

  6. mysql sum聚合函数和if()函授的联合使用

    今天去面试遇到一个数据库试题,首先说一下表结构如下: 表结构:mytest 表数据:mytest 要查询的结果如下: 在本题目中,需要用到sum聚合函数和if函数 sql如下: ,)) ,)) AS ...

  7. java以正确的方式停止线程

    java线程停止可以说是非常有讲究的,看起来非常简单,但是也要做好一些防范措施,一般停止一个线程可以使用Thread.stop();来实现,但是最好不要用,因为他是不安全的. 大多数停止线程使用Thr ...

  8. java多线程的常用方法

    介绍一些多线程中的常用方法: //启动方法 a.start(); //返回代码正在被哪个线程调用的信息 a.currentThread(); //返回线程的名字 a.currentThread().g ...

  9. vue2.x生命周期

    vue2.x生命周期 1. beforeCreate 在实例初始化之后,数据观测(data observer) 和 event/watcher 事件配置之前被调用. 2. created 实例已经创建 ...

  10. JS一个非常经典的问题:在遍历数组时对DOM监听事件,索引值将始终等于遍历结束后的值

    一个简单的Tab选项卡点击事件. <style type="text/css"> ul{padding:0;margin:0;} .tab{width:400px;} ...