华为机试题记录2
2015-07-06 22:41
411 查看
import java.util.*; import java.util.Map.Entry; public class Huawei { public static void main(String args[]){ fitOrder(); //fun(); //theLastLength(); //change(); //reversInt(); // deleteStr(); //fitOrder(); // sort(); // Scanner n = new Scanner(System.in); // // String a=""; // String b=""; // // //读取控制台输入字符串 // a=n.nextLine(); // b=n.nextLine(); // // System.out.println(a); // System.out.println(b); // // //获取字符串a,b的长度 // int aLength=a.length(); // int bLength=b.length(); // //定义最终结果字符串长度 // int length=0; // // //最终字符串的长度是a,b中较长字符串长度+1 // if(aLength>=bLength){ // length=aLength+1; // // }else { // length=bLength+1; // } // // //先将结果放到整形数组中,然后再转换 // int[] resultc = new int[length]; // // int c=0; // // int bb=0; // int aa=0; // while(length>1){ // aa=0;bb=0; // // //这里因为不确定a,b字符串哪个比较短,所以在某一个字符串加完的情况下自动用0代替。相当于将两个字符串都看作为较长字符串的长度,不够位用0补齐 // if(bLength>0){ // bb=Integer.valueOf(b.charAt(bLength-1)+""); // } // if(aLength>0){ // aa=Integer.valueOf(a.charAt(aLength-1)+""); // } // // //System.out.println("aa----"+aa+" bb-----"+bb); // c=aa+bb; // //计算出了进位后的数,再加上它前面数的进位 // resultc[length-1]+=c%10; // //进位值 // resultc[length-2]=c/10; // // length--; // aLength--; // bLength--; // } // String result=""; // // //如果结果数组的0位置的值是0,得判断舍弃 // int i =0; // if(resultc[0]==0){ // // i=1; // // } // for(;i<resultc.length;i++){ // // result+=resultc[i]; // // } // // System.out.println(result); } public static void sort(){ Scanner n = new Scanner(System.in); String in = n.nextLine(); String inArray[] = in.split(","); for(int i=0;i<inArray.length;i++){ for(int j=i;j>0&&(Integer.valueOf(inArray[j])<Integer.valueOf(inArray[j-1]));j--){ String temp = inArray[j]; inArray[j]=inArray[j-1]; inArray[j-1]=temp; } } String result=""; ArrayList<String> list = new ArrayList<String>(); for(int i=0;i<inArray.length;i++){ if(!list.contains(inArray[i])){ list.add(inArray[i]); result += inArray[i]+" "; } } System.out.println(result.substring(0,result.length()-1)); } public static void fitOrder(){ //初始化,构造hashmap存放所有命令以及执行结果 HashMap<String, String> hm = new HashMap<String, String>(); hm.put("reset", "reset what"); hm.put("reset board", "board fault"); hm.put("board add", "where to add"); hm.put("board delet", "no board at all"); hm.put("reboot backplane", "impossible"); hm.put("backplane abort", "install first"); ArrayList<String> array = new ArrayList<String>();//存放输入字符串 ArrayList<String> arraySpace = new ArrayList<String>();//存放判断字符串是否有空格 ArrayList<String> arraySpaceMap = new ArrayList<String>();//存放判断map中key字符串是否有空格 Scanner scn = new Scanner(System.in); String str; while(true){ str=scn.nextLine(); arraySpace.clear(); arraySpaceMap.clear(); arraySpace=isContainSpace(str); int size = arraySpace.size(); int count=0; //分两种情况,有空格,和无空格 String entryValue=""; String entryKey=""; if(size==1){ //遍历MAP for(Entry<String,String> entry : hm.entrySet()){ entryKey = entry.getKey(); arraySpaceMap=isContainSpace(entryKey); int mapsize=arraySpaceMap.size(); if(mapsize==1&&entryKey.startsWith(str)){ entryValue = entry.getValue(); count++; } } }else if(size==2){ for(Entry<String,String> entry:hm.entrySet()){ entryKey = entry.getKey(); arraySpaceMap=isContainSpace(entryKey); int mapsize=arraySpaceMap.size(); //如果Map没有空格,直接返回unkown command if(mapsize==1){ }else if(mapsize==2){ if(arraySpaceMap.get(0).startsWith(arraySpace.get(0))&&arraySpaceMap.get(1).startsWith(arraySpace.get(1))){ entryValue = entry.getValue(); count++; } } } } if(count==1){ System.out.println(entryValue); }else{ System.out.println("unkown command"); } } // //获取控制台输入知道输入值为空 // while (!"".equals(line=scn.nextLine())) { // array.add(line); // } // //对于每一行输入进行操作 // // for(String str : array){ // // // // } //System.out.println(substr1+" "+substr2); // } } public static ArrayList isContainSpace(String str){ ArrayList<String> list = new ArrayList<String>(); int index =0;//字符串空格位置 index = str.indexOf(" "); //如果没有空格,直接放入LIST,如果有空格,将分隔的字符串分开放入LIST if(index<0){ list.add(str); }else{ list.add(str.substring(0, index)); list.add(str.substring(index+1, str.length())); } return list; } //找到字符串中出现次数最少的字符串,删除后的字符串 public static void deleteStr(){ Scanner n = new Scanner(System.in); String str = n.nextLine(); //char[] charArray= str.toCharArray(); HashMap<String,Integer> hm = new HashMap<String,Integer>(); String s = ""; for(int i=0;i<str.length();i++){ s=str.charAt(i)+""; if(hm.containsKey(s)){ hm.put(s,hm.get(s)+1); }else hm.put(s, 1); } //System.out.println(hm); Set<Entry<String,Integer>> entryset = hm.entrySet(); Iterator iter = entryset.iterator(); Entry<String,Integer> entry = (Entry<String,Integer>)iter.next(); int smallValue = entry.getValue(); int value=0; //System.out.println(smallValue+"---------smallValue"); while(iter.hasNext()){ entry = (Entry<String,Integer>)iter.next(); value = entry.getValue(); //System.out.println(value+"---------value"); if(value<smallValue){ smallValue = value; } } //System.out.println(smallValue+"---------smallValue"); ArrayList<String> list = new ArrayList<String>(); for(Entry<String,Integer> entry_a:hm.entrySet()){ if(entry_a.getValue()==smallValue){ list.add(entry_a.getKey()); } } //System.out.println(list); for(String l:list){ str=str.replace(l, ""); } System.out.println(str); } //判断整数有几位,逆序输出 public static void reversInt(){ Scanner n = new Scanner(System.in); while(true){ int i = n.nextInt(); int length = 0; int sum = 0; while(i>0){ sum=sum*10+i%10; i=i/10; length++; } System.out.println(length+" "+sum); } } //密码转换 public static void change(){ Scanner n = new Scanner(System.in); String str = n.nextLine(); String str_new = ""; for(int i = 0;i<str.length();i++){ String s = str.charAt(i)+""; int asci = s.hashCode(); if(asci>=97&&asci<=99){ str_new+="2"; }else if(asci>=100&&asci<=102){ str_new+="3"; }else if(asci>=103&&asci<=105){ str_new+="4"; }else if(asci>=106&&asci<=108){ str_new+="5"; }else if(asci>=109&&asci<=111){ str_new+="6"; }else if(asci>=112&&asci<=115){ str_new+="7"; }else if(asci>=116&&asci<=118){ str_new+="8"; }else if(asci>=119&&asci<=122){ str_new+="9"; }else if(asci>=65&&asci<90){ str_new+=String.valueOf((char)(asci+33)); }else if(asci==90){ str_new+="a"; }else str_new+=s; } System.out.println(str_new); } //最后一个单词长度 public static void theLastLength(){ Scanner n = new Scanner(System.in); String str = n.nextLine(); int index = 0; index = str.lastIndexOf(" "); int result = str.length() - index -1; System.out.println(result); } public static void fun(){ Scanner n = new Scanner(System.in); while(true){ String str = n.nextLine(); password(str); } } //密码验证合格 public static void password(String str){ int length = str.length(); //判断长度 if(length<=8){ System.out.println("NG"); return; } int count[] = {0,0,0,0}; int sum=0; for(int i=0;i<length;i++){ int assci = (str.charAt(i)+"").hashCode(); //0~9 if(assci>=48&&assci<=57){ count[0]=1; }else if(assci>=65&&assci<=90){//A~Z count[1]=1; }else if(assci>=97&&assci<=122){//a~z count[2]=1; }else count[3]=1; } //判断是否三种组合 for(int i=0;i<4;i++){ //System.out.println(count[i]); if(count[i]==1){ sum++; } } if(sum<3){ //System.out.println("sum < 3"); System.out.println("NG"); return; } //判断重复子串 int step=1; boolean flag = true; while(step<length/2){ int i=0; for(;i<length-step;i++){ String sub = str.substring(i, i+step+1); String subl = str.substring(i+step+1); //System.out.println("sub "+sub+" subl "+subl); if(subl.contains(sub)){ //System.out.println("重复"); System.out.println("NG"); return; } } step+=1; } System.out.println("OK"); } }
相关文章推荐
- 机器学习入门资源--汇总
- 《Effective C++》读书笔记
- 删除数组中重复的数据
- 2.3 前端控制器
- Codeforces A. Vasya and Socks
- Python中的try... exception
- 如何自定义iOS中的控件 分类: ios
- 理解互斥量和信号量
- OpenWRT 常用软件安装
- maven相关
- 快速幂取模
- 路由器ACL应用
- Android 增强版百分比布局库 为了适配而扩展
- 20分制的分配
- WebService
- [LeetCode] Min Stack
- C#数据公共操作函数
- 【HM】第10课:JSP+EL表达式+JavaBean
- 为什么在属性中设置private set
- ACCESS数据库字符串连接方式