互联网公司面试题之八

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

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

答:实现代码如下:

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){}
}


运行结果:

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


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

[color=red]Input[/color]: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.

[color=blue]Output[/color]: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){}
}



运行结果:

[img]http://dl.iteye.com/upload/attachment/606596/26028105-b5b0-3d74-a1a7-134629b466f5.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值