CodeForces - 508B-Anton and currency you all know
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 oddpositive 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.
Examples
Input
527
Output
572
Input
4573
Output
3574
Input
1357997531
Output
-1
题解:找小于末尾奇数的第一个偶数,如果没有,选择最后一个偶数,没偶数则输出-1
通过这道题,我明白了strlen的时间复杂度为O(n),如果放循环内,相当于时间复杂度为O(len*n)就容易超时,所以要提前接出放外面
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int main() {
char s[100005];
scanf("%s",s);
//sort(s,s+strlen(s));
int k=-1;
int flag=0;
int minn;
for(int t=0; t<strlen(s); t++) {
if(s[t]-'0'<s[strlen(s)-1]-'0'&&(s[t]-'0')%2==0) {
k=t;
// flag=1;
break;
}
}
//cout<<k<<endl;
for(int t=0; t<strlen(s); t++) {
if((s[t]-'0')%2==0) {
minn=t;
flag=1;
}
}
//cout<<minn<<endl;
if(flag==0) {
cout<<"-1"<<endl;
return 0;
} else {
for(int t=0; t<strlen(s); t++) {
if(t==k) {
swap(s[k],s[strlen(s)-1]);
break;
}
if(k==-1) {
swap(s[minn],s[strlen(s)-1]);
break;
}
}
//printf("%s",s);//也可通过
int len=strlen(s);
for(int t=0; t<len; t++) {
printf("%c",s[t]);
}
}
return 0;
}
最新文章
- https 安全验证问题
- PowerBuilder笔记
- C#和C实现通过CRC-16 (Modbus)获取CRC值并校验数据(代码)
- javascript按中文首字母排序
- OpenJudge计算概论-比饭量【枚举法、信息数字化】
- 【BZOJ】【1010】【HNOI2008】玩具装箱Toy
- jquery ajax 后台和前台数据交互 C#
- SharePoint中 服务器发出意外响应。响应状态代码是";500";。
- workflow4.0持久化
- iOS 私有API调用
- 承接教育类html5交互课件/动画/游戏外包——如何快速开发一款html5交互课件/动画产品
- GuGuFishtion HDU - 6390 (杭电多校7E)
- 195. Spring Boot 2.0数据库迁移:Flyway
- 【转】解决configure: error: C++ compiler cannot create executables问题
- PAT 甲级 1011 World Cup Betting (20)(20 分)
- Centos7 安装Nodejs
- 数学图形(1.48)Cranioid curve头颅线
- Vue.js vs React vs Angular 深度对比[转]
- Sql Server Report Service 的部署问题
- 如何停止一个正在运行的java线程