首页 > 其他 > 详细

微软2014实习生及秋令营技术类职位在线测试+试题1+答案

时间:2014-04-13 10:11:00      阅读:462      评论:0      收藏:0      [点我收藏+]

题目1 : String reorder

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

Description

For this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’).

Your program should reorder and split all input string characters into multiple segments, and output all segments as one concatenated string. The following requirements should also be met,
1. Characters in each segment should be in strictly increasing order. For ordering, ‘9’ is larger than ‘0’, ‘a’ is larger than ‘9’, and ‘z’ is larger than ‘a’ (basically following ASCII character order).
2. Characters in the second segment must be the same as or a subset of the first segment; and every following segment must be the same as or a subset of its previous segment.

Your program should output string “<invalid input string>” when the input contains any invalid characters (i.e., outside the ‘0‘-‘9‘ and ‘a‘-‘z‘ range).


Input


Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.

Output


For each case, print exactly one line with the reordered string based on the criteria above.


样例输入
aabbccdd
007799aabbccddeeff113355zz
1234.89898
abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
样例输出
abcdabcd
013579abcdefz013579abcdefz
<invalid input string>
abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
下面是我写的答案,运行通过了。感觉很繁琐。。。
import java.io.*;
public class Main1{


	 
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		
		func1 fun=new func1();
		fun.doSomething();
		  
	}


	
}
class func1{
	int items;
	String[] array;
	String s;
	func1(){
		items=0;
		array=new String[20];
	}
	public void input() throws IOException{
		while(true){
			 s=getString();
			if(s.equals(""))
				break;
			 array[items]=s;
			 items++;
		}
	 
	}
	public static String getString() throws IOException{
		InputStreamReader isr=new InputStreamReader(System.in);
		BufferedReader br=new BufferedReader(isr);
		return br.readLine();
	}
	public boolean before(String k){
		for(int i=0;i<k.length();i++){
			 char c=k.charAt(i);
			 if((c>=‘0‘&&c<=‘9‘)||(‘a‘<=c&&c<=‘z‘)){
				 continue;
						 else{
				 return false;
			 }
		}
		return true;
	}
	public void doSomething() throws IOException{
		input();
		for(int m=0;m<items;m++){
			String p=array[m];
			String [] temp=array[m].split("");
			for(int i=0;i<temp.length-1;i++){
				temp[i]=temp[i+1];
			}
			int num=temp.length-1;
			if(!before(p)){
				System.out.println("<invalid input string>");
				continue;
			}
			while(num!=0){
				 
				 for(char j=‘0‘;j<=‘9‘;j++){
					  for(int n=0;n<num;n++){
						  if(temp[n].equals(j+"")){
							  System.out.print(temp[n]);
						  for(int k=n;k<num-1;k++){
								temp[k]=temp[k+1];
							 }
						  num--;
						  }
						  
					  }
			 }
				 for(char t=‘a‘;t<=‘z‘;t++){
					 for(int n=0;n<num;n++){
						  if(temp[n].equals(t+"")){
							  
						  System.out.print(temp[n]);
						  for(int k=n;k<num-1;k++){
								temp[k]=temp[k+1];
							 }
						  num--;
						  }
						  
					  }
				 }
 
				 
			}
			System.out.println();
			
			
		}
	}
}

微软2014实习生及秋令营技术类职位在线测试+试题1+答案,布布扣,bubuko.com

微软2014实习生及秋令营技术类职位在线测试+试题1+答案

原文:http://blog.csdn.net/huolei_blog/article/details/23555167

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!