hdu 5055 Bob and math problem

http://acm.hdu.edu.cn/showproblem.php?pid=5055

思路:贪心,先排序,然后找到一个奇数与最后以为交换,然后把前n-1位从大到小排序,看看是否符合。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int n;
 7 int a[10000];
 8 bool cmp(int a,int b)
 9 {
10    return a>b;
11 }
12 
13 int main()
14 {
15     while(scanf("%d",&n)!=EOF)
16     {
17        memset(a,0,sizeof(a));
18        bool flag=false;
19        for(int i=1; i<=n; i++)
20        {
21           scanf("%d",&a[i]);
22           if(a[i]%2)
23           {
24              flag=true;
25           }
26        }
27        if(!flag)
28        {
29           printf("-1
");
30        }
31        else
32        {
33           sort(a+1,a+n+1,cmp);
34           for(int i=n; i>=1; i--)
35           {
36               if(a[i]%2)
37               {
38                   swap(a[i],a[n]);
39                   break;
40               }
41           }
42           sort(a+1,a+n,cmp);
43           int j=1;
44           while(a[j]==0)
45           {
46               j++;
47           }
48           if(j>1)
49           {
50              printf("-1
");
51              continue;
52           }
53           for(int i=j; i<=n; i++)
54           {
55               printf("%d",a[i]);
56           }
57           printf("
");
58        }
59     }
60     return 0;
61 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/4277943.html