`
YuHuang.Neil
  • 浏览: 181366 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

互联网公司面试题之八

阅读更多
问题一:读入两个小于100的正整数A和B,计算A+B。需要注意的是:A和B的每一位数字由对应的英文单词给出。

例如:
one + two =
three four + five six =
zero seven + eight nine =
zero + zero =
当A和B同时为0时输入结束,相应的结果不要输出

答:实现代码如下:
import java.util.*;
public class Main {	
	static{
		Scanner s=new Scanner(System.in);
		String stop="zero + zero =";
		String[] match=new String[]{"zero","one","two","three",
				"four","five","six","seven","eight","nine"};
		for(;s.hasNext();){
			String str=s.nextLine();
			if(stop.equals(str)) break;
			else{
				String[] p=str.split("\\+");
				StringBuilder sb;
				int a[]=new int[2];
				int t=0;
				for(String m : p){
					sb=new StringBuilder();
					String[] sub=m.trim().split(" ");
					for(String j:sub){
						for(int i=0;i<match.length;++i){
							if(j.contains(match[i])){
								sb.append(i+"");
							}
						}
				    }
					a[t++]=Integer.valueOf(sb.toString());
				}
			    System.out.println(a[0]+a[1]);
			}
		}
	}
	public static void main(String[] args){}
}


运行结果:




问题二:You are given an unsorted array of integer numbers. Your task is to sort this array and kill possible duplicated elements occurring in it.

Input:For each case, the first line of the input contains an integer number N representing the quantity of numbers in this array(1≤N≤1000). Next N lines contain N integer numbers(one number per each line) of the original array.

Output:For each case ,outtput file should contain at most N numbers sorted in ascending order. Every number in the output file should occur only once.


答:实现代码如下:

import java.util.*;
public class Main {	
	static{
		Scanner s=new Scanner(System.in);
		Set<Integer> set;
		for(;s.hasNext();){
			set = new TreeSet<Integer>();
			int n=s.nextInt();
			for(int i=0;i<n;++i){
				set.add(Integer.valueOf(s.nextInt()));
			}
			Iterator<Integer> it=set.iterator();
			System.out.print((Integer)it.next());
			for(;it.hasNext();){
				System.out.print(" "+(Integer)it.next());
			}
			System.out.println();
		}
	}
	public static void main(String[] args){}
}



运行结果:




  • 大小: 25.5 KB
  • 大小: 24.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics