Anton and currency you all know

B. Anton and currency you all know
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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
Copy
527
output
572
input
Copy
4573
output
3574
input
Copy
1357997531
output
-1

这题题意其实很好理解 就算看不懂英文,看懂一半然后再看看样例就能明白了...只不过这里有几个细节需要注意一下

题意:给我们一个数(2<=n<=1e5),然后要求我们改变两个位置上的数字使得这个数变成偶数,并且是最大的.

那么我就当成一串数字了.emmm那么怎么办呢,

分析:首先要判断它能不能变成一个even,也就是说这个串数里有没有偶数.for循环遍历一下啦

        其次要保证交换了之后形成的新数字是最大的偶数.()emmm要加点料了.)

        那么问题来了,怎么保证呢.

思路:如果要这个数字最大,那么我们得保证较大的数字放在前面,那么我们交换的时候是不是得考虑一下.

        考虑一下两个交换数字的大小.

        我们也知道一个数 从左边到右边 权值是递减的(十万,万,千,百,十,个)所以,越靠右的权值越小

        那么在for循环里,如果有比末位数小的比如末位数是9

        那么我们要尽量使得较大的9在越前面越好所以一旦找到比末位数小的要立刻停止搜索.

        (比如说6429)得到的最大的是(9426)而不是(6492)

        但是如果没有一个数比末尾数小呢???比如说末位数是1.

        因为没有比末位数小的,所以,我们要将交换后的比较小的末位数1放在越后面越好

        比如说(6421)得到的最大值为6412而不是1426

        那么思路就这样成型了.

        那么先不看代码试试打着看???

接下来,就是看代码能力的时候了.

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string>
#define maxn 100000
using namespace std;
string s;
int main()
{
    cin>>s;
    int n=s.size();
    int j=-1;//标记偶数的位置.
    int flag=0;//标记是否已经找到比末位小的数.
    for(int i=0;i<n;i++)
    {
        if((s[i]-'0')%2==0)//首先,如果是偶数
        {
            if(s[i]-'0'<s[n-1]-'0')//其次如果是比末尾小的数.
            {
                char temp=s[i];//那么交换了
                s[i]=s[n-1];
                s[n-1]=temp;
                flag=1;//并且标记.
                break;//如果找到比末位小的 立刻停止搜索
            }
            j=i;//标记偶数,一遍for循环后,得到的便是最后一个偶数.
        }
    }
    if(!flag&&j!=-1)//如果没有比末位小的,但是有偶数
    {
        char temp=s[j];
        s[j]=s[n-1];
        s[n-1]=temp;
    }
    
    if(!flag&&j==-1)//如果既没有比末位小的,并且没有偶数.为什么要并且呢,因为上面我如果第一个就是比末位小的,那么会直接跳出,不会标记找到偶数了
        cout<<"-1"<<endl;
    else
    {
        for(int i=0;i<n;i++)
            cout<<s[i];
        cout<<endl;
    }
}

ps:一开始没有想到那个break;一直wa在测试9.多亏工作室大佬发现,我才意识到我的思路有纰漏.

emmm2018年4月2日 20:08:41

放了好久的题,今天猜a了写出来

心情复杂,加呼吸道发炎,十分难受了,

南阳空气和天气是真的烂,我是绝对不会留在这里的.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值