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;
    }
  • 相关阅读:
    Post请求的两种编码格式:application/x-www-form-urlencoded和multipart/form-data传参方式
    工作中常用的JavaScript函数片段
    解决导入导出Excel表格文字乱码问题
    清空antd-design时间选择组件 RangePicker的值
    react.js Hooks路由跳转
    linux跳板机服务器搭建
    docker及docker-compose学习
    Android Jenkins+Git+Gradle持续集成
    Windows Server 2008 R2常规安全设置及基本安全策略
    ubuntu lnmp安装及php扩展
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781877.html
Copyright © 2011-2022 走看看