最近的笔试较多,把做过的笔试题做一个大致的总结(编程题):
H3C笔试题:
question_1:1,2,3,4,四个数,通过组合成一个不重复的三位数,共有多少种组合方式,并将其输出。
public class H3C_question1{ public static void main(String[] args){ int i = 0; for(int numOne = 1; numOne <= 4; numOne++){ for(int numTwo = 1; numTwo <= 4; numTwo++){ for(int numThree = 1; numThree <= 4; numThree++){ if(numOne != numTwo && numTwo != numThree && numOne != numThree){ i++; int num = numOne * 100 + numTwo * 10 + numThree; System.out.println(num); } } } }
System.out.println("共有" + i + "个不重复的组合。");
} }
# -*- coding: utf-8 -*- count = 0 for numOne in range(1, 5): for numTwo in range(1, 5): for numThree in range(1, 5): if numOne != numTwo and numTwo != numThree and numOne != numThree: count += 1 num = numOne * 100 + numTwo * 10 + numThree print(numThree) print('共有' + str(count) + '个不重复的组合。')
question_2:对于一个文本表达式如"((a+b) * (c + (d+e)))",来看它的括号是否匹配,如果匹配则输出括号最大嵌套层数。 如")()()"或"(a+b))*(c+d)"这样的都是括号不匹配的。
public class H3C_question2{ public static void main(String[] args){ int leftCount = 0; int rightCount = 0; int max = 0; String txt = "((a + b) * (c + (d + e)))"; for(int i = 0; i < txt.length(); i++){ if(txt.charAt(i) == '('){ leftCount++; }else if(txt.charAt(i) == ')'){ rightCount++; if(leftCount == 0){ System.out.println("不匹配");
break; }else{ if(max < leftCount){ max = leftCount; } leftCount--; rightCount--; } }else{ continue; } } if(leftCount == 0 && rightCount == 0){
System.out.println("匹配,且最大嵌套层数为:" + max);
}else{
System.out.println("不匹配");
}
} }
# -*- coding: utf-8 -*-
def max_CengShu(s): leftCount = 0 rightCount = 0 max = 0 for i in range(len(s)): if s[i] == '(': leftCount += 1 elif s[i] == ')': rightCount += 1 if leftCount == 0: print('不匹配') break else: if max < leftCount: max = leftCount leftCount -= 1 rightCount -= 1 else: continue if leftCount == 0 and rightCount == 0: print('匹配,且最大嵌套层数为:' + str(max)) else: print('不匹配')
s = '((a+b) * (c + (d+e)))'
max_CengShu(s)
print('-------------------------------------------------')
s = '((a+b) * (c + (d+e))'
max_CengShu(s)
print('-------------------------------------------------')
s = '(a+b) * (c + (d+e)))'
max_CengShu(s)
print('-------------------------------------------------')
s = '(a+b) * (c + (d+e))'
max_CengShu(s)
print('-------------------------------------------------')
华为笔试题:
question_1:字符串压缩,对输入字符串进行压缩,输入"aaabcccdde",输出"3ab3c2de",即对连续出现的字符进行压缩。
思路:使用索引i来遍历字符串,对每个当前索引i的字符进行计数(用count),遍历的过程中用一个while来判断索引i和i+1的两个字符是否相等,如果相等,则count+1且i+1,不相等,则跳出while,判断count的值。然后继续遍历字符串。
public class HuaWei_question1{ public static String string_compress(String s){ int length = s.length(); int count = 0; int i; String retStr = ""; if(length == 0){ return "字符串为空"; } for(i = 0; i < length; i++){ count = 1; if(i == length - 1){ // 避免出现length-1和length进行比较(会超出字符串长度) retStr = retStr + s.charAt(i); break; } while(s.charAt(i) == s.charAt(i + 1)){ count++; i++; if(i == length - 1){ // 避免出现length-1和length进行比较(会超出字符串长度) break; } } if(count > 1){ retStr = retStr + count + s.charAt(i); }else{ reStr = retStr + s.charAt(i); } } } public static void main(String[] args){ String s = "aaabbbbc"; String s2 = "aaabcccdde"; String retS = string_compress(s); System.out.pirntln(retS); Stirng retS2 = string_compress(s); System.out.println(retS2); } }
# -*- coding: utf-8 -*- def string_compress(s): length = len(s) retStr = '' if length == 0: return '字符串为空' for i in range(length): count = 1 if i == length - 1: retStr = retStr + s[i] break; while s[i] == s[i + 1]: count += 1 i += 1 if i == length - 1: break; if count > 1: retStr = retStr + str(count) + s[i] else: retStr = retStr + s[i] # print(retStr) return retStr s = "aaabbbbc" retS = string_compress(s) print(retS)
这个题的python版本是错误的,这是因为python和java在for循环性质上的错误:
对于java的for循环,第一次i = 0,经过一次循环后,在循环体中i被改变了,因此在下一次循环的时候,i会在这个改变的基础上再加1。
而对于python的for循环,第一次i = 0,经过一次循环后,在循环体中i被改变了,但是在下一次循环的时候,i是在i=0的基础上加1,即i为1。
即在python的for循环中i不会因为循环体中语句对它的改变而改变。
question_3:在一个无序的数字列表中,找到它最大长度的子序列(子序列是从小到大排序的)。
思路:找出该数字列表中所有我从小打到排列的子序列,然后找到最长的那一个即可。
在java中要用到集合类ArrayList
package test;import java.util.Arrays; import java.util.ArrayList; public class Huawei_question3 { /* * 题目:在一个无序的数字列表中,找到它最大长度的子序列(子序列是从小到大排序的)。 * 思路:找出该数字列表中所有我从小打到排列的子序列,然后找到最长的那一个即可。 */ public static int maxSubList(ArrayList<Integer> list){ ArrayList<ArrayList<Integer>> arrayLists = new ArrayList<ArrayList<Integer>>(); int length = list.size(); for(int i = 0; i < length - 1; i++){ ArrayList<Integer> arrayList = new ArrayList<Integer>(); for(int j = i; j < length; j++){ if(arrayList.isEmpty()){ arrayList.add(list.get(j)); }else{ if(list.get(j) > arrayList.get(arrayList.size() - 1)){ arrayList.add(list.get(j)); } } } arrayLists.add(arrayList); } int maxlength = arrayLists.get(0).size(); for(int i = 0; i < arrayLists.size(); i++){ if(arrayLists.get(i).size() > maxlength){ maxlength = arrayLists.get(i).size(); } } for(int i = 0; i < arrayLists.size(); i++){ System.out.println(arrayLists.get(i)); } return maxlength; } public static void main(String[] args){ int [] array = {3, 2, 5, 6, 8, 7, 9, 4, 1}; int [] array2 = {7, 2, 3, 1, 5, 8, 9, 6}; ArrayList<Integer> arrayList = new ArrayList<Integer>(); for(int i = 0; i < array.length; i++){ arrayList.add(array[i]); } int maxlength = maxSubList(arrayList); System.out.println(maxlength); int maxlength2 = maxSubList(arrayList2); System.out.println(maxlength2); } }
# -*- coding: utf-8 -*- def max_SubList(list_num): length = len(list_num) lists = [] for i in range(length-1): li = [] for j in range(i, length): if li == []: li.append(list_num[j]) else: if list_num[j] > li[len(li) - 1]: li.append(list_num[j]) lists.append(li) for i in range(len(lists)): print(lists[i]) maxlength = len(lists[0]) for i in range(len(lists)): if len(lists[i]) > maxlength: maxlength = len(lists[i]) return maxlength l = [3, 2, 5, 6, 8, 7, 9, 4, 1] maxlength = max_SubList(l) print('最长的从小到大的子序列长度为:' + str(maxlength))
// 避免出现length-1和length进行比较(会超出字符串长度)