YuHuang.Neil 发表于 2013-2-3 13:39:14

互联网公司面试题之八

问题一:读入两个小于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;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)){sb.append(i+"");}}    }a=Integer.valueOf(sb.toString());}    System.out.println(a+a);}}}public static void main(String[] args){}}
运行结果:

http://dl.iteye.com/upload/attachment/606455/0719af0a-be42-3d2d-98cf-a7a39a5263d9.jpg


问题二: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){}}

运行结果:

http://dl.iteye.com/upload/attachment/606596/26028105-b5b0-3d74-a1a7-134629b466f5.jpg
页: [1]
查看完整版本: 互联网公司面试题之八