华为Java上机题 2013年 [广州]
2013-08-31 21:46
344 查看
1、第一题是一串字符,拼音的1~9,输出相应数字,比如输入yiersansiwuliuqibajiu,输出123456789
实现如下:
方法: outputNum(String inputString)
第一种方法:
第二种方法:
2、假设1900年1月1日是星期一,然后让你输出之后的****年*月*日是星期几。
方法:getWeek(Stirng date); //其中date的格式为 yyyy-MM-dd
方法1:[此方法理论上是正确的,但是大概在1970年之前得到的星期都是有误的,1970年之后就是正确的]
原帖如下:
************************************************************************************************
-----------------------------------------------------------------------------------------------------------------
回答:
************************************************************************************************
方法2:
3、第三题,给定背包容量,还有若干件东西的体积,问能不能从中取出一些,恰好填满背包,如果能,输出该取出哪些,数据量比较小,最多20件东西,且最多有一种填满的办法。
背包体积:50
假设输入:"2,5,6,7,1,3,4,9,8,11,23,13,16,26"
输出为:23,26,1
暂时未做。
实现如下:
方法: outputNum(String inputString)
第一种方法:
public static void outputNum(String inputString){ List<String> strliList = new ArrayList<String>(); String ouput = ""; for (int i = 0; i < inputString.length(); i++) { strliList.add(inputString.charAt(i)+""); } while(strliList.size()>0){ switch (strliList.get(0).charAt(0)) { //ouput 1 case 'y': if (strliList.size()>1 && strliList.get(1).equals("i")) { ouput += "1"; strliList.remove(0); strliList.remove(0); }else { strliList.remove(0); } break; //ouput 2 case 'e': if (strliList.size()>1 && strliList.get(1).equals("r")) { ouput += "2"; strliList.remove(0); strliList.remove(0); }else { strliList.remove(0); } break; //ouput 3 4 case 's': if (strliList.size()>2 && strliList.get(1).equals("a") && strliList.get(2).equals("n") ) { ouput += "3"; strliList.remove(0); strliList.remove(0); strliList.remove(0); }else if(strliList.size()>1 && strliList.get(1).equals("i")){ ouput += "4"; strliList.remove(0); strliList.remove(0); }else { strliList.remove(0); } break; //ouput 5 case 'w': if (strliList.size()>1 && strliList.get(1).equals("u")) { ouput += "5"; strliList.remove(0); strliList.remove(0); }else { strliList.remove(0); } break; //ouput 6 case 'l': if (strliList.size()>2 && strliList.get(1).equals("i") && strliList.get(2).equals("u")) { ouput += "6"; strliList.remove(0); strliList.remove(0); strliList.remove(0); }else { strliList.remove(0); } break; //ouput 7 case 'q': if (strliList.size()>2 && strliList.get(1).equals("i") ) { ouput += "7"; strliList.remove(0); strliList.remove(0); }else { strliList.remove(0); } break; //ouput 8 case 'b': if (strliList.size()>2 && strliList.get(1).equals("a") ) { ouput += "8"; strliList.remove(0); strliList.remove(0); }else { strliList.remove(0); } break; //ouput 9 case 'j': if (strliList.size()>2 && strliList.get(1).equals("i") && strliList.get(2).equals("u")) { ouput += "9"; strliList.remove(0); strliList.remove(0); strliList.remove(0); }else { strliList.remove(0); } break; default: strliList.remove(0); break; } } System.out.println(ouput); }
第二种方法:
public static void outputNum(String inputString){ inputString = inputString.replaceAll("yi", "1"); inputString = inputString.replaceAll("er", "2"); inputString = inputString.replaceAll("san", "3"); inputString = inputString.replaceAll("si", "4"); inputString = inputString.replaceAll("wu", "5"); inputString = inputString.replaceAll("liu", "6"); inputString = inputString.replaceAll("qi", "7"); inputString = inputString.replaceAll("ba", "8"); inputString = inputString.replaceAll("jiu", "9"); System.out.println(inputString); }
2、假设1900年1月1日是星期一,然后让你输出之后的****年*月*日是星期几。
方法:getWeek(Stirng date); //其中date的格式为 yyyy-MM-dd
方法1:[此方法理论上是正确的,但是大概在1970年之前得到的星期都是有误的,1970年之后就是正确的]
public static void getWeekday(String date){ String weekday = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date d1 = sdf.parse("1900-1-1"); //星期一 Date d2 = sdf.parse(date); int inteval = Integer.parseInt( ""+(d2.getTime()- d1.getTime() ) /(1000*60*60*24)); int day = inteval+ 1; int w = -1; //基准日期之前 if (d1.after(d2)) { w = (7-day%7)%7; }else { w = day%7; } switch (w) { case 1: weekday = "星期1"; break; case 2: weekday = "星期2"; break; case 3: weekday = "星期3"; break; case 4: weekday = "星期4"; break; case 5: weekday = "星期5"; break; case 6: weekday = "星期6"; break; case 0: weekday = "星期天"; break; default: weekday = "无法判断"; break; } } catch (Exception e) { e.printStackTrace(); } System.out.println(weekday); }关于方法1的错误请参考:http://zhidao.baidu.com/link?url=ROb_TEs9AggLJ6TpCLWpz1CXa3LmBfR5uRcJnYgIGvg79-9KtPecCaafjj1i5fHPealdAG3hUWfT77-Vx2fCoq
原帖如下:
************************************************************************************************
问题:Java中getTime()日期时间计算的问题
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str3 = "1900-01-31 00:00:00"; String str7 = "1940-01-31 00:00:00"; Date sDt3 = sf.parse(str3); Date sDt7 = sf.parse(str7); long ld3 = sDt3.getTime() /1000; long ld7 = sDt7.getTime() /1000; 计算出来后 ld7 - ld3 = 1262217952 (秒) 但是两者的日期差为14609,而14609*24*60*60=1262217600.为什么有出入? 因此采用ld3+14609*24*60*60,Date(),sf.format()后得到的结果就为1940-01-30 23:54:08,请问采用getTime()取到值后+一定的秒数得到一个新的时间,应该怎么处理呢?如果str7的时间为1920年的话算出来的时间却是对的,为什么,是哪里的小细节没有注意到还是有什么特殊的处理
上面这种实现方式是哪里不对呢? 测试发现1927-12-31 00:00:00与1928-01-01 00:00:00只差一天24*60*60=86400秒,但是通过getTime()取值差却为86752,与上面值一致,就是说在27年到28年时发生了变化。
-----------------------------------------------------------------------------------------------------------------
回答:
确实是一个值得思考的问题。我只发现浅层次的直接原因:精确到秒 这个差异是由 SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 在解析特定日期 1927-12-31 23:54:07到1927-12-31 23:54:08造成的。 这“一秒钟”内发生突变,相差352000毫秒,约5分52秒。 就能解释你“得到的结果就为1940-01-30 23:54:08"的误差 用Calendar和GregorianCalendar类验证,结果相同。 是Java的错误,还是历史规定,没有查到相关资料,深层次的原因不明。
************************************************************************************************
方法2:
//输入的日期格式为 yyyy-MM-dd public static void getWeekday2(String date){ Calendar cal1=Calendar.getInstance(); cal1.set(1900,0, 1); //星期一 int year = 0; int month = 0; int day = 0; int w=-1; String weekday=""; String[] s = date.split("-"); //对输入的格式进行检查 if(s.length<3){ System.out.println("日期格式有误,正确日期格式为 yyyy-MM-dd"); return; } try { year = Integer.parseInt(s[0]); month = Integer.parseInt(s[1]); day = Integer.parseInt(s[2]); if (year<0 || 12<month || month<1 || day>31 || day<0 ) { System.out.println("日期格式有误,正确日期格式为 yyyy-MM-dd"); return; } } catch (Exception e) { System.out.println("日期格式有误,正确日期格式为 yyyy-MM-dd"); } Calendar cal2=Calendar.getInstance(); cal2.set(year,month-1 , day); //获取输入日期到1900-1-1的相隔天数 int interval = Integer.parseInt(""+ (cal2.getTime().getTime()-cal1.getTime().getTime())/(1000*60*60*24)) +1; System.out.println( "相差几天:"+interval ); // 输入日期在1900年之前 if (cal2.getTime().before(cal1.getTime())) { w = (7-interval%7)%7; }else { w = interval%7; } switch (w) { case 1: weekday = "星期1"; break; case 2: weekday = "星期2"; break; case 3: weekday = "星期3"; break; case 4: weekday = "星期4"; break; case 5: weekday = "星期5"; break; case 6: weekday = "星期6"; break; case 0: weekday = "星期天"; break; default: weekday = "无法判断"; break; } System.out.println(weekday); }
3、第三题,给定背包容量,还有若干件东西的体积,问能不能从中取出一些,恰好填满背包,如果能,输出该取出哪些,数据量比较小,最多20件东西,且最多有一种填满的办法。
背包体积:50
假设输入:"2,5,6,7,1,3,4,9,8,11,23,13,16,26"
输出为:23,26,1
暂时未做。
相关文章推荐
- 2013年华为校园招聘上机题(java)
- 华为Java上机题
- 华为java上机试题
- 华为上机试题(java)找出一个字符串中出现次数最多的字符
- 两道华为上机题目解答(Java语言)
- 华为校招上机笔试试题(B卷)——java实现
- 2011华为上机试题-Java
- 华为上机题3——词语接龙(深搜java版本)
- 2012 年 华为上机题 java 约瑟夫环
- 华为上机测试题(协议数据解析-java)
- 华为上机题整理1——Java实现
- 华为上机Java实现
- 华为上机测试题(地铁换乘-java)
- 华为上机测试题(MP3光标移动-java)
- 华为上机测试题(求亮灯数量-java)
- 华为2013年西安java机试题目:如何过滤掉数组中的非法字符。
- 华为上机测试题(大数相乘-java)
- 华为校园招聘上机试题 Java实现(二)
- 华为2013年的一题上机笔试题(关于字符串)
- 华为上机 字符串合并处理 java