HDU 5055 Bob and Math Problem

Source: Internet
Author: User
Bob and Math Problem

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 961 accepted submission (s): 368


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 [math processing error].

 

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 input30 1 335 4 232 4 6

 

Sample Output301425-1

 

Source bestcoder round #11 (div. 2) algorithm analysis: giving you n numbers allows n numbers to form a qualified digital output. If the group does not come out, the output is-1. condition 1. an odd number. Condition 2. No leading 0. Condition 3. ensure the maximum number of items that meet the first two conditions! Algorithm Implementation: 1. check whether there are any odd numbers in the N number. If-1 is not directly output. 2. check the number of 0. If the number of 0 is greater than or equal to n-1, the number in the group will either have a leading 0 or an odd number and be directly output-1. 3. place the smallest odd number in the final output, and output the remaining numbers in the ascending order. 4. If n = 1, that is to say, if only one number is input, the data is output directly if the odd number is input. Otherwise, the data is output-1.
# Include <iostream> # include <stdio. h> # include <math. h> # include <string. h ># include <algorithm> using namespace STD; int main () {int N, mm, nn; int A [101]; int FF [101]; int I, J; int EE, Oo, DD; while (scanf ("% d", & N )! = EOF) {If (n = 1) {scanf ("% d", & NN); If (NN % 2 = 0) {printf ("-1 \ n"); continue;} else if (NN % 2 = 1) {printf ("% d \ n", NN ); continue ;}} EE = 0; oo = 0; dd = 0; Mm = 999999; for (I = 0; I <n; I ++) {scanf ("% d", & A [I]); if (a [I] % 2 = 1) {oo ++; // odd if (a [I] <mm) {mm = A [I] ;}} else if (a [I] % 2 = 0) {EE ++; if (A [I] = 0) dd ++; // Number of 0 }}if (OO = 0) {printf ("-1 \ n "); continue;} If (DD >=n-1) {printf ("-1 \ n"); continue;} memset (FF, 0, sizeof (ff); sort (, A + n); for (I = 0; I <n; I ++) {if (a [I] = mm) {FF [I] = 1; break ;}for (I = n-1; I> = 0; I --) {If (FF [I] = 0) printf ("% d ", A [I]);} printf ("% d \ n", mm);} return 0 ;}

 

HDU 5055 Bob and Math Problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.