2015华为招聘机试题及答案

来源:互联网 发布:网络赚钱平台 编辑:程序博客网 时间:2024/04/29 00:34
import java.util.Scanner;/** * 判断一个字符串是否是首字母大写且非首字母小写。 输入:一个任意字符串,长度不超过128个字符 * 输出:如果输入字符串首字符为大写字母且其他字符非大写字母,返回true * 其他情况(空字符串、首字符非字母、首字母为小写、首字母大写但其他字符非字母等)均返回false * 样例输入:Hello world * 样例输出:true * @author yutao *  */public class StrJudge {public static void main(String[] args) {Scanner input = new Scanner(System.in);String src = input.nextLine();input.close();System.out.println(judge(src));}public static boolean judge(String src) {if (src.charAt(0) < 'A' || src.charAt(0) > 'Z') {return false;} else {for (int i = 1; i < src.length() - 1; i++) {if ((src.charAt(i) < 'a' || src.charAt(i) > 'z')&& src.charAt(i) != ' ') {return false;}}return true;}}}
<pre class="java" abp="655" name="code">/** * 根据来电号码显示人名 * 当手机来电时会根据电话号码自动匹配保存在电话本中的人名,进行电话号码匹配时一般会根据号码的后多少位进行匹配, * 因为不同的网络环境下发的号码可能会不同,比如,有的地区显示区号,有的可能不显示区号等。该题目要求如下: * 对来电号码进行后N位的匹配,N通过参数读取获得,输出所有匹配号码的人名,多个人名间加上空格 *  参数1: int型,可能的值为 0,7,8,9,当为0时需要对来电号码进行精确匹配,也就是说电话本中的号码和来电号码必须一摸一样才满足 *  参数2:字符串,为来电号码 *  匹配的人名,号码匹配失败时显示"unknown"(不包括引号) *  样例输入:7 09282357 *  样例输出:Jane Cora * 提示: * 来电的号码长度不会小于要匹配的长度 * 电话本中号码如果小于要匹配的长度,要跳过,不进行比较 * 匹配是从字符串后面匹配,比如号码12345678,当进行7位匹配时,只比较后7位,也就是2345678 * 电话号码本使用如下的数据,直接写到代码里: * Person persons[] = {new Person("Jack", "13572105859"),new Person("Tom", "008602989282356"),new Person("Ada", "02989282356"),new Person("Alice", "89282356"),new Person("Carmen", "01089282356"), new Person("Frances", "123"),new Person("Jane", "89282357"),new Person("Selina", "18220191828"),new Person("Ellen", "18220191828"),new Person("Kitty", "456781234"), new Person("Cora", "09282357"),new Person("0", "0")};*/ public class PhoneMatch {static Person persons[] = { new Person("Jack", "13572105859"),new Person("Tom", "008602989282356"),new Person("Ada", "02989282356"),new Person("Alice", "89282356"),new Person("Carmen", "01089282356"), new Person("Frances", "123"),new Person("Jane", "89282357"),new Person("Selina", "18220191828"),new Person("Ellen", "18220191828"),new Person("Kitty", "456781234"), new Person("Cora", "09282357"),new Person("0", "0")};public static void main(String[] args) {System.out.println(getName(0, "008602989282356"));}public static String getName(int count, String phone) {String result = "";if (count == 0) {//精确匹配for (Person p : persons) {if (p.phone.equals(phone)) {result += p.name + " ";}}} else {//count=7,8,9String temp1 = "";//来电号码截取count位后的字符串String temp2 = "";//本地号码截取count位后的字符串int len1 = phone.length();//来电号码位数int len2 = 0;//当前被比较的号码本中的号码位数for (Person p : persons) {len2 = p.phone.length();if ( len1 == len2) {if (len1 < count) {if (p.phone.equals(phone)) {result += p.name + " ";}}else {temp1 = phone.substring(len1 - count);temp2 = p.phone.substring(len2 - count);if (temp2.equals(temp1)) {result += p.name + " ";}}} else {if (len1 >= count && len2 >= count ) {temp1 = phone.substring(len1 - count);temp2 = p.phone.substring(len2 - count);if (temp2.equals(temp1)) {result += p.name + " ";}}}}}return result.equals("") ? "unknown" : result.substring(0,result.length()-1);}public static class Person {private String name;private String phone;public Person(String name, String phone) {this.name = name;this.phone = phone;}}}


0 0
原创粉丝点击