7月面试笔试记录
算法题
1.将一个16进制的字符串转发为整型的数字
1 /**** 2 * 将16进制字符串转化为10进制的整型 3 * 4 * @param src 5 * @return 6 */ 7 public static double parseInt(String src) { 8 double result = 0; 9 int size = src.length(); 10 for (int i = size - 1, j = 0; i >=0; i--, j++) { 11 result += charToInt(src.charAt(i)) * Math.pow(16, j); 12 } 13 14 return result; 15 } 16 17 public static int charToInt(char c) { 18 int result = 0; 19 switch (c) { 20 case 'A'|'a': 21 result = 10; 22 break; 23 case 'B'|'b': 24 result = 11; 25 break; 26 case 'C'|'c': 27 result = 12; 28 break; 29 case 'D'|'d': 30 result = 13; 31 break; 32 case 'E'|'e': 33 result = 14; 34 break; 35 case 'F'|'f': 36 result = 15; 37 break; 38 default: 39 result = Integer.parseInt(c + ""); 40 break; 41 } 42 return result; 43 }
jdk自带的方法一句话:Integer.valueOf(字符串,16)
一起举例:
- 十进制转成十六进制:Integer.toHexString(int i)
- 十进制转成八进制: Integer.toOctalString(int i)
- 十进制转成二进制:Integer.toBinaryString(int i)
2.将1000以内能将3整除但不是偶数的找出来,并由大到小排序,ps 必须用到冒泡排序
public static void main(String[] args) { List<Integer> result = new ArrayList<Integer>(); for (int i = 0; i < 1001; i++) { if (i % 3 == 0 && i % 2 != 0) { result.add(i); } } for (int i = 0; i < result.size(); i++) { for (int j = 0; j < result.size()-1; j++) { int temp = result.get(j); if (result.get(j) < result.get(j + 1)) { result.set(j, result.get(j + 1)); result.set(j + 1, temp); } } } System.out.println(result); }
数据库
1.查询表table1的数据字段number,除掉重复的数据并以number排序
select distinct t.number from table1 as t order by t.number
2.查询表table1的数据字段number,重复次数为2的,并排序
select t.number ,count(*) from table1 as t group by t.number having count(*)>1