Thursday, May 7, 2015

508B - Anton and currency you all know (Codeforces)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define pf printf
#define sf scanf
#define loop(i, n) for(i=0; i<(n); i++)
#define MAX 100

int main()
{
    string s;
    int i, len;
    int index, temp, odd;
    int flag=0;
    cin >> s;
    len = s.length();
    odd = s[len-1]-'0';
    for(i=len-1; i>=0; i--)
    {
        temp = s[i]-'0';
        if(temp%2==0)
        {
            flag++;
            if(flag==1)
            {
                index = i;
            }
            if(odd>temp)
            {
                index = i;
            }
        }
    }
    if(flag==0)
        cout <<"-1" << endl;
    else
    {
        swap(s[index], s[len-1]);
        cout << s << endl;
    }
    return 0;
}

No comments:

Post a Comment

Compare equality of two string in C

#include <stdio.h> #include<string.h> int main() {     char* country = "Bangladesh";     char* country2;     ...