Berland, 2016. The exchange rate of currency you all know against the burle has increased so much that to simplify the calculations, its fractional part was neglected and the exchange rate is now assumed to be an integer.
Reliable sources have informed the financier Anton of some information about the exchange rate of currency you all know against the burle for tomorrow. Now Anton knows that tomorrow the exchange rate will be an even number, which can be obtained from the present rate by swapping exactly two distinct digits in it. Of all the possible values that meet these conditions, the exchange rate for tomorrow will be the maximum possible. It is guaranteed that today the exchange rate is an odd positive integer n. Help Anton to determine the exchange rate of currency you all know for tomorrow!
Input
The first line contains an odd positive integer n — the exchange rate of currency you all know for today. The length of number n’s representation is within range from 2 to 105, inclusive. The representation of n doesn’t contain any leading zeroes.
Output
If the information about tomorrow’s exchange rate is inconsistent, that is, there is no integer that meets the condition, print - 1.
Otherwise, print the exchange rate of currency you all know against the burle for tomorrow. This should be the maximum possible number of those that are even and that are obtained from today’s exchange rate by swapping exactly two digits. Exchange rate representation should not contain leading zeroes.
Examples
Input
527
Output
572
Input
4573
Output
3574
Input
1357997531
Output
-1
题意:就是把这个奇数 交换两个数变成最大的偶数
WR思路:刚开始没读懂吧算是,我想到了两种 情况竟然啊!!!一种是奇数,另一种是偶数,我真是个憨蛋啊,还能读错题,我第一种情况就行了啊。当我意识到我第一个错误的时候我是这样想的
接下来展示我的超时代码
我寻思就是挨个换呗 反正数不多,没想到超时了
string s;
cin >> s; int l = s.size();
int f = 0;
int gg = 0; string sx = s; string ss="\0";
for (int i = 0; i < l - 1; i++)
{
char s1, s2;
if ((s[i] - '0') % 2 == 0)
{
s1 = s[i]; s2 = s[l - 1];
s[i] = s[l - 1]; s[l - 1] = s1;
ss = ss>s?ss : s;
s[l - 1] = s2; s[i] = s1;
}
}
if (ss != "\0")
cout << ss;
else cout << -1;
然后看别人的思路 反正不就是最后一个跟前面的偶数交换吗
首先就是你得想法让大的数字上前头啊
就是当末尾奇数大于(从第一位开始)前面的偶数,那这俩就交换
要是前面的偶数都比末尾大,那就交换 最低位的偶数就行了
就这么简单地问题我竟然还犯这么多的错误啊!!!!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include <queue>
using namespace std;
char s[100020];
int main()
{
cin >>s;
bool a = 0;
int longth = strlen(s);
for (int i = 0; i <longth; i++)
{
if ((s[i] - '0') % 2 == 0 && s[i]<s[longth - 1])
{
a = 1;
char t;
t = s[i];
s[i] = s[longth - 1];
s[longth-1] = t;
break;
}
}
if (a==0)
{
for (int i=longth-1; i >= 0; i--)
{
if ((s[i] - '0') % 2 ==0)
{
a = 1;
char t;
t = s[i];
s[i] = s[longth - 1];
s[longth-1] = t; break;
}
}
}
if (a)
{ cout<<s;
}
else cout << -1;
}