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!
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.
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.
527
572
4573
3574
1357997531
-1
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!
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.
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.
527
572
4573
3574
1357997531
-1
CF上的#include<stdio.h> #include<string.h> char s[100100]; int b[100100]; int main() { int i; while(~scanf("%s",s)) { int found=0; getchar(); int n=strlen(s); for(i=0;i<n;i++) { b[i]=s[i]-'0'; } for(i=0;i<n;i++) { if(b[i]%2==0)//如果存在一个比最后一位数大的偶数就交换 { if(b[i]<b[n-1]) { int t=b[i]; b[i]=b[n-1]; b[n-1]=t; found=1; break; } } } if(found!=1) { for(i=n-1;i>=0;i--) { if(b[i]%2==0) { if(b[i]>b[n-1])//在不存在比最后一位大的前提下找最后一个比他小的偶数交换 { int t=b[i]; b[i]=b[n-1]; b[n-1]=t; found=2; break; } } } } if(found==0)//当不存在偶数数据时 printf("-1"); if(found==1||found==2) for(i=0;i<n;i++) printf("%d",b[i]); printf("\n"); } return 0; }
一个大水题:题目要求,将一个奇数只变换一个位置,变成最大的偶数。