/*
题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大
贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记);2. 有偶数但都比末尾数字小(x位置标记)
仿照别人写的,再看自己的代码发现有清晰的思维是多重要
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
using namespace std; const int MAXN = 1e5 + ;
const int INF = 0x3f3f3f3f;
char s[MAXN]; int main(void) //Codeforces Round #288 (Div. 2) B. Anton and currency you all know
{
#ifndef ONLINE_JUDGE
freopen ("B.in", "r", stdin);
#endif scanf ("%s", &s); int len = strlen (s);
bool flag = false;
int x = -;
for (int i=; i<len-; ++i)
{
if ((s[i] - '') % == )
{
x = i;
if (s[i] < s[len-])
{
swap (s[i], s[len-]);
flag = true; break;
}
}
} if (!flag)
{
swap (s[x], s[len-]);
}
(x != -) ? printf ("%s\n", s) : puts ("-1"); return ;
}
if (len == )
{
puts ("-1");
}
else if (len == )
{
if ((s[] - '') % != ) puts ("-1");
else
{
printf ("%c%c\n", s[], s[]);
}
}
else if (len == )
{
if ((s[] - '') % == )
{
if ((s[] - '') % == )
{
if (s[] > s[]) swap (s[], s[]);
else swap (s[], s[]);
printf ("%s\n", s);
}
else
{
swap (s[], s[]);
printf ("%s\n", s);
}
}
else if ((s[] - '') % == )
{
swap (s[], s[]);
printf ("%s\n", s);
}
else puts ("-1");
}
else
{
char ch = ''; int x = -;
for (int i=len-; i>=; --i)
{
if ((s[i]-'') % == )
{
if (ch <= s[i])
{
if (ch == s[i])
{
if (s[i] < s[len-])
{
ch = s[i]; x = i;
}
}
else
{
ch = s[i]; x = i;
}
}
}
}
if (x == -) puts ("-1");
else
{
swap (s[x], s[len-]);
printf ("%s\n", s);
}
}
思维混乱的代码