codeforces - 508B - Anton and currency you all know(贪心)
来源:互联网 发布:微课录屏制作软件 编辑:程序博客网 时间:2024/05/29 04:43
codeforces - 508B - Anton and currency you all know(贪心)
Link:http://codeforces.com/contest/508/problem/B
题意:
给出一个长度小于105的奇数,交换其中两位,使其变成一个偶数,若有多种做法输出最大的。若不能构造出一个偶数,输出-1。
解法:
贪心。一开始以为只需交换最高位偶数即可,秒送wa。
正确解法是找出最高位的比最末位奇数小的偶数,交换它们。
若没有,交换最低位的偶数与最末位奇数。
#include <bits/stdc++.h>#define inf 0x3f3f3f3f#define max(a,b) a>b?a:b#define min(a,b) a>b?b:ausing namespace std;char a[100010];int main(){ gets(a); bool f=0; int l=strlen(a); for(int i=0;i<l;i++) { if((a[i]-'0')%2==0&&a[i]<a[l-1]) { f=1; swap(a[i],a[l-1]); break; } } if(!f) { for(int i=l-1;i>=0;i--) { if((a[i]-'0')%2==0) { f=1; swap(a[i],a[l-1]); break; } } } if(f) puts(a); else puts("-1"); return 0;}
0 0
- codeforces #508B# Anton and currency you all know(贪心)
- codeforces - 508B - Anton and currency you all know(贪心)
- Codeforces 508B. Anton and currency you all know
- codeforces 508 B. Anton and currency you all know
- codeforces 508B Anton and currency you all know
- CF 505B(Anton and currency you all know-贪心)
- B. Anton and currency you all know
- B. Anton and currency you all know
- B. Anton and currency you all know
- CodeForces 508B Round #288 div2B Anton and currency you all know
- B. Anton and currency you all know(Codeforces Round #288 (Div. 2))
- codeforces--A - Pasha and Pixels--B - Anton and currency you all know
- Codeforces Round #288 (Div. 2) B. Anton and currency you all know
- Codeforces Round #288 (Div. 2) B. Anton and currency you all know
- Codeforces Round #288 (Div. 2) B - Anton and currency you all know
- B. Anton and currency you all know(Codeforces Round #288 (Div. 2))
- Codeforces Round #288 (Div. 二) Anton and currency you all know
- Codeforces-734B-Anton and Digits(贪心)
- 运算符、选择结构、循环结构
- 前台 JSON对象转换成字符串 相互转换 的几种方式
- 文件的简单操作
- 【图论】【单源最短路】【dijikstra】pascal+邻接矩阵/边集数组
- JSP——1
- codeforces - 508B - Anton and currency you all know(贪心)
- NodeJS基础API搭建服务器详细过程记录
- 【总结】软件工程视频(一)
- Android 加载动画(关于SpinKit的使用)
- leetcode_413. Arithmetic Slices 数组中连续的等差数列个数
- 第1章 ECS
- linux shell编程之——if语句
- 构建 Android APP 一定要绕过的 30 个坑
- wikioi 2315 longge的问题 2012年省队选拔赛山东(欧拉函数应用)