您的位置:首页 > 其它

B. Anton and currency you all know

2015-01-28 15:05 363 查看
B. Anton and currency you all knowtime limit per test0.5 secondsmemory limit perB. Anton and currency you all knowtime limit per test0.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBerland, 2016. The exchange rate ofcurrency 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 ofcurrency 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. Ofall 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 anodd positive integern. Help Anton to determine the exchange rate ofcurrency you all know for tomorrow!InputThe first line contains an odd positive integern — the exchange rate of currency you all know for today. The length of numbern's representation is within range from2 to 105, inclusive. The representation ofn doesn't contain any leading zeroes.OutputIf 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 ofcurrency 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 leadingzeroes.Sample test(s)Input
527
Output
572
Input
4573
Output
3574
Input
1357997531
Output
-1
test256 megabytesinputstandard inputoutputstandard outputBerland, 2016. The exchange rate ofcurrency 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 ofcurrency 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. Ofall 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 anodd positive integern. Help Anton to determine the exchange rate ofcurrency you all know for tomorrow!InputThe first line contains an odd positive integern — the exchange rate of currency you all know for today. The length of numbern's representation is within range from2 to 105, inclusive. The representation ofn doesn't contain any leading zeroes.OutputIf 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 ofcurrency 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 leadingzeroes.Sample test(s)Input
527
Output
572
Input
4573
Output
3574
Input
1357997531
Output
-1
CF上的[code]    #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;

}
一个大水题:题目要求,将一个奇数只变换一个位置,变成最大的偶数。[/code]

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: