2013年8月华为java机试题目

先吐槽一下。我是前段时间通过华为官方投的简历。华为最近几天有一轮招聘,通过短信形式通知的,让应聘者回复参加哪种语言,然后竟然没有确认收到信息。等了大概一周华为才通过短信通知去西工大老校区机试,有的没有收到短信。结果本来是下午三点开考,我一点半出发,两点就到了。结果等到了四点才开考!那么热的天,实在受不了了!无语! 考试题目就三道题: 1.给你一个int型数组,按降序排序。我就直接用的冒泡。 2.给你一个String字符串,比如 !!!###*%H***U///AWEI***”123!!! 过滤掉其中非字母和非数字的字符,然后将剩下的输出。 3.给你一个String,比如 aaabbddefasaaddd 返回数目最多的字符。如果有数目相同的字符,随机返回一个即可。 附上我自己写的代码:
package com.duapp.itfanr;

public class CharDemo {

	public static void main(String[] args) {

		String str = "abcdefabccrrrrehhhhhhhrrrcccc" ;

		System.out.println( "最多的字母之一为:"+"------------->"+getMostTimes(str ) ) ;

	}

	static char getMostTimes(String str ) {

		int len = str.length() ;
		StringBuffer sb = new StringBuffer(str.charAt(0)) ;
		for(int i=1; i<len; i++){			
			String strTemp = new String(sb) ;
			if(!strTemp.contains(String.valueOf(str.charAt(i)))) {
				sb.append(str.charAt(i)) ;				
			}			
		}
		String charAll = new String(sb) ;//得到去重时候的字符串
		int len2 = charAll.length() ;
		int []count = new int[len2];
		for(int i=0; i<charAll.length(); i++){
			count[i] = 0 ;

		}
		System.out.println(charAll) ;//统计个数
		for(int i =0;i<len; i++){
			for(int j =0; j<len2; j++){
				if(str.charAt(i)==charAll.charAt(j)){					
					count[j]++;  
				}
			}

		}
		for(int i = 0;i<len2; i++){
		System.out.println(charAll.charAt(i)+"的个数为-------------->"+ count[i]) ;
		}				

		int max = count[0]; //寻找最大值所在的位置
		int maxFlag = 0;
		for(int i = 0; i<len2 ; i++){
		   if(count[i]>max){
			   max = count[i] ;
			   maxFlag = i ;			   
		   }		    
		}		

		return charAll.charAt(maxFlag) ;
	}

}
结果如下:
bcdefarh
b的个数为-------------->2
c的个数为-------------->7
d的个数为-------------->1
e的个数为-------------->2
f的个数为-------------->1
a的个数为-------------->2
r的个数为-------------->7
h的个数为-------------->7
最多的字母之一为:------------->c
 

转载于:https://my.oschina.net/itfanr/blog/195644

猜你喜欢

转载自blog.csdn.net/weixin_33733810/article/details/91799610
今日推荐