HDU ---- (5055) Bob and math problem (Greedy)

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): 401 accepted submission (s): 149


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

 

Sample Output301425-1

 

Source bestcoder round #11 (Div. 2) greedy code:
 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<functional> 5 #include<iostream> 6 using namespace std; 7 int str[120]; 8 int main() 9 {10   int n,i,res;11   while(scanf("%d",&n)!=EOF)12   {13       res=10;14       for(i=0;i<n;i++)15     {16        scanf("%d",&str[i]);17       if(str[i]&1==1&&res>str[i])18           res=str[i];19     }20     if(res==10){21         printf("-1\n");22         continue;23     }24     sort(str,str+n,greater<int>());25      int fir=-1,st=res;26     for(i=0;i<n;i++)27     {28         if(res==str[i]) res=-1;29         else30         {31             fir=str[i];32             break;33         }34     }35     if(fir==0) printf("-1\n");36     else37     {38         if(fir!=-1)39           printf("%d",fir);40         for( i++; i<n;i++)41         if(res==str[i])res=-1;42         else43          printf("%d",str[i]);44         printf("%d\n",st);45     }46   }47   return 0;48 }
View code

 

HDU ---- (5055) Bob and math problem (Greedy)

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.