Codeforces Round #288 (Div. 2) B - Anton and currency you all know

来源:互联网 发布:爸爸的网络用语称呼 编辑:程序博客网 时间:2024/06/05 11:05

昨晚做的cf比较失败,在B题卡住了,当时一看,水题!咔咔的写完代码,各种错。


总结一下,B题之所以会出现这种情况,是由于自己做题少,考虑不全面造成的,明明知道怎么做,可考虑不全面,在没有测试数据的情况下,不知道自己错在哪,还是自己做题少的原因!

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

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char str[100100];int a[100100],b[100100],c[100100];int main(){    while(scanf("%s",str)!=EOF)    {        int flag=0;        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        int len=strlen(str);        int k=0,tmp,x=0;        for(int i=0;i<len;i++)        {            a[i]=str[i]-'0';            if(a[i]%2==0)                {                    b[k++]=a[i];                    c[x++]=a[i];                }        }        if(k==1)//只有一个偶数        {            for(int i=0;i<len;i++)            {                if(a[i]==b[0])                {                    flag=1;                    tmp=a[len-1];                    a[len-1]=a[i];                    a[i]=tmp;                    break;                }            }        }        else if(k>1)//偶的个数>1        {            sort(c,c+x);        if(a[len-1]<c[0])        {            for(int i=len-1;i>0;i--)            {                if(b[k-1]==a[i])                {                    flag=1;                    tmp=a[len-1];                    a[len-1]=a[i];                    a[i]=tmp;                    break;                }            }        }        else        {            for(int j=0;j<k;j++)            {                for(int i=j;i<len;i++)                {                    if(b[j]==a[i])                    {                        if(a[i]<a[len-1])                        {                             flag=1;                             tmp=a[len-1];                             a[len-1]=a[i];                             a[i]=tmp;                             if(flag)                             break;                        }                     }                }                if(flag)                break;            }        }        }        else//没有偶数            flag=0;        if(flag)        {            int i=0,r;            for(i=0;i<len;i++)        {            if(a[i]==0)                continue;            if(a[i]!=0)                break;        }        for(r=i;r<len;r++)            printf("%d",a[r]);        printf("\n");        }        else            printf("-1\n");    }    return 0;}


0 0
原创粉丝点击