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;
}
CodeForces - 508B-Anton and currency you all know的更多相关文章
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- Codeforces Round #288 (Div. 2) B. Anton and currency you all know 贪心
B. Anton and currency you all know time limit per test 0.5 seconds memory limit per test 256 megabyt ...
- 【codeforces 508B】Anton and currency you all know
[题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- Codeforces 593B Anton and Lines
LINK time limit per test 1 second memory limit per test 256 megabytes input standard input output st ...
- Codeforces Round #313 A Currency System in Geraldion
A Currency System in Geraldion Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64 ...
- Codeforces 734F Anton and School(位运算)
[题目链接] http://codeforces.com/problemset/problem/734/F [题目大意] 给出数列b和数列c,求数列a,如果不存在则输出-1 [题解] 我们发现: bi ...
- [刷题]Codeforces 785D - Anton and School - 2
Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...
- Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]
题目链接:https://codeforces.com/problemset/problem/785/D 题解: 首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 ...
- Codeforces 584E - Anton and Ira - [贪心]
题目链接:https://codeforces.com/contest/584/problem/E 题意: 给两个 $1 \sim n$ 的排列 $p,s$,交换 $p_i,p_j$ 两个元素需要花费 ...
随机推荐
- Java企业微信开发_10_未验证域名归属,JS-SDK功能受限
1.现象: 在企业微信后台填写可信域名后,提示:未验证域名归属,JS-SDK功能受限,如下图: 点击“申请域名校验”后, 注意:域名根目录 当时一直不清楚这个域名根目录在哪里,最后让我给试出来了 2. ...
- JavaScript里值比较的方法
JavaScript里值比较的方法 参考资料 一张图彻底搞懂JavaScript的==运算 toString()和valueof()方法的区别 Object.is 和 == 与 === 不同 == 运 ...
- leetcode 110 Balanced Binary Tree(DFS)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Posix线程编程指南(2)
这是一个关于Posix线程编程的专栏.作者在阐明概念的基础上,将向您详细讲述Posix线程库API.本文是第2篇将向您讲述线程的创建与取消. 一.概念及作用在单线程程序中,我们经常要用到"全 ...
- 51nod_1236_序列求和 V3 _组合数学
51nod_1236_序列求和 V3 _组合数学 Fib(n)表示斐波那契数列的第n项,Fib(n) = Fib(n-1) + Fib(n-2).Fib(0) = 0, Fib(1) = 1. (1, ...
- Visual Studio 编译后去掉只读属性
Visual Studio 编译后去掉只读属性 attrib $(TargetPath) -R attrib $(TargetDir)$(TargetName).pdb -R
- openStack灾备方案说明
本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1) OpenStack 高可用方案概述 (2) Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3 ...
- Robot Framework基础学习(五)
Selenium2Library 常用关键字介绍 关于Selenium2Library 的关键字,我们可以参考:http://rtomac.github.io/robotframework-selen ...
- day1 java基础回顾-集合
1.集合 1.1 集合的类型与各自的特性 ---|Collection: 单列集合 ---|List: 有存储顺序, 可重复 ---|ArrayList: 数组实现, 查找快, 增删慢 由于是数组实现 ...
- Java中的Synchronized关键字用法
认识synchronized 对于写多线程程序的人来说,经常碰到的就是并发问题,对于容易出现并发问题的地方加上synchronized修饰符基本上就搞定 了,如果说不考虑性能问题的话,这一招绝对能应对 ...