2016年CVTE笔试编程题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiao__jia__jia/article/details/82453551

                                          2016年CVTE笔试编程题

编程题1:

给你一个字符串,让你求最大的哪个对称子字符串;
题解:就是求最长 子串是否为回文串。

import java.util.Scanner;


class Main{
	static int len = 0;
	static int max = 0;
	public static void main(String[] args)  {
    Scanner in = new Scanner(System.in);
   while(in.hasNext()) {
	   String s = in.next();
	   len = s.length();
	   if(len == 0) {
		   System.out.println(len);
		   continue;
	   }
	   max = 0;
	   for(int i = 0; i < len; i++) {
		   fun(s, i, i);
		   fun(s, i, i+1);
	   }
	   System.out.println(max);
   }
	
	}
	public static void fun(String s, int i, int j) {
		
		while( i >= 0 && j < len && s.charAt(i) == s.charAt(j)){
		       i--;
		       j++;
		}
	
		if(max < j -i-1) {
			max = j -i-1;
		}
		
	}
}


 

编程题2:

求一个字符串的全排列组合。
直接递归求解。

public class AllSort{  
 
    public static void main(String[] args) 
    {  
 
        char buf[]={'a','b','c'};  
        perm(buf,0,buf.length-1);  
 
    }  
 
    public static void perm(char[] buf,int start,int end)
    {  
        if(start==end)
        {
            for(int i=0;i<=end;i++)
            {  
                System.out.print(buf[i]);  
            }  
            System.out.println();     
        }  
        else
        {
            for(int i=start;i<=end;i++)
            {  
                char temp=buf[start];
                buf[start]=buf[i];
                buf[i]=temp;  
                perm(buf,start+1,end);              
                temp=buf[start];            
                buf[start]=buf[i];
                buf[i]=temp;  
            }  
        }  
    }  
}


 

猜你喜欢

转载自blog.csdn.net/xiao__jia__jia/article/details/82453551
今日推荐