zoukankan      html  css  js  c++  java
  • CodeForces

    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;
    }
  • 相关阅读:
    [转]顶点数据压缩
    [转]将某个Qt4项目升级到Qt5遇到的问题
    「05」回归的诱惑:一文读懂线性回归
    AI漫谈:我们距离实现《庆余年》里的五竹叔机器人还有多远?
    “木兰”去哪儿了?被全国700所中小学引入的国产编程语言“木兰”,为何在官网删除了下载链接
    有哪些让人相见恨晚的Python库(一)
    2019年最值得关注的AI领域技术突破及未来展望
    为什么样本方差的分母是n-1?为什么它又叫做无偏估计?
    「04」机器学习、深度学习需要哪些数学知识?
    「03」机器学习、深度学习该怎样入门?
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781877.html
Copyright © 2011-2022 走看看