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;
    }
  • 相关阅读:
    Selenium IDE
    selenium简介
    Java程序要操作数据库,一定要使用JDBC技术吗?
    JDBC几个接口分区叫什么?它们分别有什么用?
    JDBC的全称是什么?
    什么时候使用session?什么时候使用application?
    内置对象session和application的作用是什么?
    修改表单中为什么要使用隐藏域?
    如何创建一个不能修改的属性?
    如何修改属性?
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781877.html
Copyright © 2011-2022 走看看