codeforces - 508B - Anton and currency you all know(贪心)
2016-11-14 20:45
399 查看
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:a using 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; }
相关文章推荐
- 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-贪心)
- CodeForces 508B Round #288 div2B 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. 二) Anton and currency you all know
- Codeforces Round #288 (Div. 2) B. Anton and currency you all know
- 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 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))
- B. Anton and currency you all know(Codeforces Round #288 (Div. 2))
- codeforces--A - Pasha and Pixels--B - Anton and currency you all know
- Physical and Logical Block Corruptions. All you wanted to know about it. (文档 ID 840978.1)
- Codeforces-734B-Anton and Digits(贪心)