Codeforces Round #288 (Div. 2) B. 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.

Sample test(s)

input

527

output

572

input

4573

output

3574

input

1357997531

output

-1

题意:给一个长度不超过100000的奇数,要求交换任意两位上的数字,使它成为一个最大的偶数。

思路:字符串读入,记录下最后一位数上的奇数a,从前往后扫,找到第一个比a小的偶数,将它和a交换就是结果,若没有找到比a小的偶数,就将a和最后一个偶数交换。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
typedef long long ll;
using namespace std;

char str[maxn];

int main()
{
    char ch;
    while (~scanf("%s",str))
    {
        int len=strlen(str);
        int heven=0;    //标记是否有偶数
        int minn=INF;
        int pos;     //记录第一个比最末尾奇数小的偶数的位置
        int first=1;
        int posmo;   //记录最靠后的偶数的位置
        int last=str[len-1]-'0';
        for (int i=0;i<len;i++)
        {
            int a=str[i]-'0';
            if (a%2==0)
            {
                posmo=i;
                heven=1;
                if (first&&a<last)
                {
                    pos=i;
                    first=0;
                    break;
                }
            }
        }
        if (heven==0)
            printf("-1\n");
        else
        {
            if (!first)
            {
                ch=str[len-1];
                str[len-1]=str[pos];
                str[pos]=ch;
            }
            else
            {
                ch=str[len-1];
                str[len-1]=str[posmo];
                str[posmo]=ch;
            }
            printf("%s\n",str);
        }
    }
    return 0;
}
时间: 2024-11-05 23:38:32

Codeforces Round #288 (Div. 2) B. Anton and currency you all know的相关文章

Codeforces Round #253 (Div. 2) A. Anton and Letters

题目很简单,只需要注意带空格的输入用getline即可 #include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> using namespace std; int main(){ string str; getline(cin,str); set<char> a; for(int i= 1 ; i < s

Codeforces Round #288 (Div. 2)

A. Pasha and Pixels 题目大意:给出n*m的棋盘,初始为全白,每次在上面使一个格子变黑,问多少次的时候使得棋盘上出现2×2的黑格 思路:模拟 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<queue> 5 #define maxn 900000 6 #define LL long long 7 using namespace std; 8 boo

Codeforces Round #288 (Div. 2) 待续

A. Pasha and Pixels ( 暴力 ) 题意:给一个n*m的矩阵染色(初始为全白),如果在k步之内染出一个2*2的矩阵,输出最小步数,否则输出0 分析:brute force #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int M[ 1010 ][ 1010 ]; int step, r, c, k

Codeforces Round #329 (Div. 2)B. Anton and Lines 贪心

B. Anton and Lines The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = ki·x + bi. It was necessar

Codeforces Round #379 (Div. 2) C. Anton and Making Potions(二分)

Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare npotions. Anton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two typ

Codeforces Round #379 (Div. 2) D. Anton and Chess(模拟)

Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check

Codeforces Round #288 (Div. 2)B(字符串)

题意:交换两个数,使得交换后的数是偶数且尽可能大: KEY:分情况,1末尾数为偶数,即找到比它小的偶数交换,假如没有比它小的偶数,不用交换.2末尾数是奇数,再分情况,<1>全都是奇数(这个可以在一开始就判断掉),即输出-1,<2>有一个偶数,无论如何谁大谁小都要交换,<3>全部偶数都比奇数大,从n - 2(n是字符串长度)开始找到一个偶数交换即可,<4>如果有一些偶数比末尾数大,有些比它小,即从0开始找到比奇数小的那个偶数交换即可.其实是分类讨论.(有更好的

Codeforces Round #288 (Div. 2) ABCDE

A题 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <vector> 7 #include <map> 8 #include <queue> 9 10 using namespace std; 11 12 #define LL

Codeforces Round #288 (Div. 2) A,B,C,D,E

A:一个一个点向图里面加,判断其所在的位置与其他的点是否可以构成小矩形就可以了. B:贪心,如果前面的偶数有比他小的就找到一个最靠前的交换,如果前面的偶数都比它小,就找一个最靠后的交换. C:贪心,把蜡烛尽可能的放在恶魔,来之前,这样可以充分利用蜡烛的时间,模拟一下,不停地向前方就可以了.如果蜡烛时间小于需要的数目,一定不可以. const int maxn = 2010; int vis[maxn]; int num[maxn]; int main() { int m, t, r; while