最近面试了几家公司,但是都没碰见机试题。自己在网上找了几道试了试。。。。。。。。。
1、求数组中出现次数最多的项
1 import java.util.HashMap; 2 import java.util.Map; 3 import java.util.Scanner; 4 5 public class Practice { 6 7 /** 8 * 求数组中出现次数最多的项 9 */ 10 public static void main(String[] args) { 11 getMax(); 12 } 13 14 public static void getMax() { 15 Scanner sc = new Scanner(System.in); 16 System.out.println("请输入一串字符:"); 17 String str = sc.nextLine(); 18 Character key = null; 19 Map<Character, Integer> map = new HashMap<>(); 20 for (int i = 0; i < str.length(); i++) { 21 key = str.charAt(i); 22 if (map.get(key) != null) { 23 map.put(key, map.get(key) + 1); 24 } else { 25 map.put(key, 1); 26 } 27 } 28 int maxValue = 0; 29 char maxKey = ' '; 30 for (Map.Entry<Character, Integer> max : map.entrySet()) { 31 System.out.println("字符\'"+max.getKey() + "\'-----" + max.getValue()+"个"); 32 if (max.getValue() > maxValue) { 33 maxValue = max.getValue(); 34 maxKey = max.getKey(); 35 } 36 } 37 System.out.println("最多的字符是:" + maxKey + "----共记:" + maxValue+"个"); 38 } 39 }
2、输入一串字符,去掉中间位置的‘*’,保留首尾的‘*’
1 public class Demo { 2 public static void main(String[] args){ 3 4 Scanner sc=new Scanner (System.in); 5 System.out.println("请输入字符串:"); 6 String str=sc.next(); 7 StringBuilder sb=new StringBuilder(str); 8 for(int i=1;i<sb.length()-1;i++){ 9 if(sb.charAt(i)=='*'){ 10 sb.deleteCharAt(i); 11 i--; 12 } 13 } 14 System.out.println(sb); 15 } 16 }
3、输入一个字符串,将其对应的ASCLL值加5后,输出结果,若加5后值大于z,将其转换为从a开始的字符
1 public class Demo { 2 public static void main(String[] args){ 3 Scanner sc=new Scanner (System.in); 4 System.out.println("请输一串小写字母:"); 5 String str=sc.next(); 6 StringBuilder sb=new StringBuilder(); 7 char[] arr=str.toCharArray(); 8 for(int i=0;i<arr.length;i++){ 9 char c=(char) (arr[i]+5); 10 if(c>'z'){ 11 c='a'; 12 } 13 sb.append(c); 14 } 15 System.out.println(sb); 16 } 17 }
4、通过键盘输入一串小写字母(a~z)组成的字符串。
请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。比如字符串“abacacde”过滤结果为“abcde”
1 Scanner sc=new Scanner(System.in); 2 System.out.println("请输入运算式:(输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开)"); 3 String str=sc.nextLine(); 4 String [] s=str.split(" "); 5 int result=0; 6 if(s.length>3){ 7 result=0; 8 }else{ 9 int a =Integer.valueOf(str.substring(0,1)); 10 int b=Integer.valueOf(str.substring(4,5)); 11 String op=str.substring(2,3); 12 switch(op){ 13 case "+": 14 result=a+b; 15 break; 16 case "-": 17 result=a-b; break; 18 default: 19 System.out.println("I don't konw"); 20 } 21 } 22 System.out.println(result);
5、通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
输入字符串的格式为:“操作数1 运算符 操作数2”, “操作数”与“运算符”之间以一个空格隔开。
1 Scanner sc=new Scanner(System.in); 2 System.out.println("请输入运算式:(输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开)"); 3 String str=sc.nextLine(); 4 String [] s=str.split(" "); 5 int result=0; 6 if(s.length>3){ 7 result=0; 8 }else{ 9 int a =Integer.valueOf(str.substring(0,1)); 10 int b=Integer.valueOf(str.substring(4,5)); 11 String op=str.substring(2,3); 12 switch(op){ 13 case "+": 14 result=a+b; 15 break; 16 case "-": 17 result=a-b; break; 18 default: 19 System.out.println("I don't konw"); 20 } 21 } 22 System.out.println(result);
6、设有n个正整数,将他们连接成一排,组成一个最大的多位整数。 如:n=3时,3个整数13,312,343,连成的最大整数为34331213。如:n=4时,4个整数7,13,4,246连接成的最大整数为7424613。
1 public static void getM() { 2 Scanner sc = new Scanner(System.in); 3 int n = sc.nextInt(); 4 int[] arr = new int[n]; 5 for (int i = 0; i < arr.length; i++) { 6 arr[i] = sc.nextInt(); 7 } 8 // 9 for (int x = 0; x < n - 1; x++) { 10 for (int y = 0; y < n - 1 - x; y++) { 11 if (compareIt(arr[y], arr[y + 1]) < 0) { 12 int temp = arr[y + 1]; 13 arr[y + 1] = arr[y]; 14 arr[y] = temp; 15 } 16 } 17 } 18 for (int i = 0; i < n; i++) { 19 System.out.print(arr[i]); 20 } 21 } 22 //自己写的比较方法,比较出组合数字最大的 23 public static int compareIt(int a, int b) { 24 String s1 = a + ""; 25 String s2 = b + ""; 26 int ta = Integer.valueOf(s1 + s2); 27 int tb = Integer.valueOf(s2 + s1); 28 return ta - tb; 29 }
当年我是自来卷,现在想变成自来熟,但是。。。成了程序猿。。。