Coderforces 508B 简单贪心

背景:1——WA:有一出少了break,还是惧怕时间没有自己出数据看看的错!

思路:fi全是奇数,输出-1,else if 全部偶数都大于最后一位数,让最右边的偶数和最后一位数交换;else 让最左边的比最后一位数打的偶数与最后一位数交换!

#include<stdio.h>
#include<string.h>
char str[100009];

int main(void){
		while(~scanf("%s",str)){
			int n=strlen(str);
			bool ok=false;
			for(int i=0;i < n-1;i++){if((str[i]-'0')%2 == 0) ok=true;}
			if(!ok) printf("-1\n");
			else{
				for(int i=0;i < n-1;i++){
					if((str[i]-'0')%2 == 0 && str[i] < str[n-1]){
						char key=str[i];
						str[i]=str[n-1];
						str[n-1]=key;
						ok=false;
						break;
					}
				}
				if(!ok) printf("%s\n",str);
				else{
					for(int i=n-2;i >= 0;i--){
						if((str[i]-'0')%2 == 0){
							char key=str[i];
						  str[i]=str[n-1];
						  str[n-1]=key;
						  printf("%s\n",str);
						  break;
						}
					}
				}
			}
		}
		return 0;
} 
时间: 2024-10-12 19:56:19

Coderforces 508B 简单贪心的相关文章

POJ 2393 Yogurt factory(简单贪心)

http://poj.org/problem?id=2393 题意:任务规定,一个酸奶制造厂,在n个星期内,分别要向外提供y[i]unit的酸奶.已知这个制造厂第i周制造每unit酸奶的费用为c[i],储存室储存每1unit酸奶1星期的费用为s.问要完成这个任务的最小费用是多少. . . . . . . . . . . . . . 思路:简单贪心.维护一个目前最优的代价minc = min(c, minc + s),然后求和. #include <iostream> using namespa

POJ 3069 Saruman&#39;s Army (简单贪心)

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5343   Accepted: 2733 Description Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep track of his forces, Saruman distributes se

USACO SECTION1 2.1 Milking Cows 简单贪心

题目链接: http://train.usaco.org/usacoprob2?a=p8taXWtZBpU&S=milk2 题目描述: 给出n组数, 求出最长连续有数区间与最长连续无数区间 解题思路: 简单贪心, 先排序, 然后如果有交集判断右端点,如果右端点大则延伸, 否则continue, 如果无交集更新两个ans. 代码: http://train.usaco.org/usacoprob2?a=p8taXWtZBpU&S=milk2 思考: 简单的贪心我的代码却WA了, 记住一切从简

hdu 2037简单贪心--活动安排问题

活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动能兼容地使用公共资源 设有n个活动的集合E={1,2,…,n},其中每个活动都要求使用同一资源,如演讲会场等,而在同一时间内只有一个活动能使用这一资源.每个活动i都有 一个要求使用该资源的起始时间si和一个结束时间fi,且si <fi .如果选择了活动i,则它在半开时间区间[si, fi)内占用资源

POJ-3069-Saruman&#39;s Army(Java简单贪心)

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4699   Accepted: 2430 Description Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep track of his forces, Saruman distributes se

HDU 1009:FatMouse&amp;#39; Trade(简单贪心)

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41982    Accepted Submission(s): 13962 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

HDU 1009:FatMouse&#39; Trade(简单贪心)

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41982    Accepted Submission(s): 13962 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

ACM_ICPC hdu-2111(简单贪心算法)

一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; struct CT{ int pi; int mi; }; int cmp( CT p1 , CT p2 ){ return p1.pi > p2.pi ; } int main() { int sum , V , n

UVA 11292 Dragon of Loowater(简单贪心)

Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predato