Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5055
Problem descriptionrecently, 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 the odd integer is "301 ".
Inputthere 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) $.
Outputthe 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
30 1 335 4 232 4 6
Sample output
301425-1
Sourcebestcoder round #11 (Div. 2)
Official question:
The Code is as follows:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){ int n; int a[117]; while(~scanf("%d",&n)) { int minn = 10; int flag = 0; int tt = 0; for(int i = 0; i < n; i++) { scanf("%d",&a[i]); if(a[i]&1) { flag = 1; if(a[i] < minn) { minn = a[i]; tt = i; } } } a[tt] = 10; sort(a,a+n); if(!flag) { printf("-1\n"); continue; } flag = 0; for(int i = n-2; i >= 0; i--) { if(a[i]==0 && !flag) continue; flag = 1; printf("%d",a[i]); } if(flag || n==1) printf("%d\n",minn); else printf("-1\n"); } return 0;}
HDU 5055 Bob and math problem (constructor)