互联网公司面试题之八_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 互联网公司面试题之八

互联网公司面试题之八

 2011/12/16 9:08:46  YuHuang.Neil  http://yuhuang-neil.iteye.com  我要评论(0)
  • 摘要:问题一:读入两个小于100的正整数A和B,计算A+B。需要注意的是:A和B的每一位数字由对应的英文单词给出。例如:one+two=threefour+fivesix=zeroseven+eightnine=zero+zero=当A和B同时为0时输入结束,相应的结果不要输出。答:实现代码如下:importjava.util.*;publicclassMain{static{Scanners=newScanner(System.in);Stringstop="zero+zero="
  • 标签:面试 公司 互联网 面试题
问题一:读入两个小于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
  • 查看图片附件
发表评论
用户名: 匿名