您的位置:首页 > 其它

hdu 5055 Bob and math problem

2015-02-06 20:09 323 查看
http://acm.hdu.edu.cn/showproblem.php?pid=5055

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

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int n;
int a[10000];
bool cmp(int a,int b)
{
return a>b;
}

int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
bool flag=false;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
if(a[i]%2)
{
flag=true;
}
}
if(!flag)
{
printf("-1\n");
}
else
{
sort(a+1,a+n+1,cmp);
for(int i=n; i>=1; i--)
{
if(a[i]%2)
{
swap(a[i],a
);
break;
}
}
sort(a+1,a+n,cmp);
int j=1;
while(a[j]==0)
{
j++;
}
if(j>1)
{
printf("-1\n");
continue;
}
for(int i=j; i<=n; i++)
{
printf("%d",a[i]);
}
printf("\n");
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: