机试题详解

前端之家收集整理的这篇文章主要介绍了机试题详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.找出如下数组中的最大元素和最小元素。并指出该元素在数组中的位置。
array[][]= {{4,1,22},{6,7,3,15},{18},{11,9}}

分析:此题主要是考双重for循环的二维数组的遍历(容易级别)

代码实现如下:

class  Test2
{
	public static void main(String[] args) 
	{
		int [][]a = {{4,9}};
		int max=a[0][0],min=a[0][0],xi=0,xj=0,ni=0,nj=0;
		for(int i =0;i<a.length;i++){
			for(int j=0;j<a[i].length;j++){
				if(max<a[i][j]){
					max=a[i][j];
					xi=i;
					xj=j;
				}
				if(min>a[i][j]){
					min=a[i][j];
					ni=i;
					nj=j;
				}
			}
		}
		System.out.println("max:"+max+" position:"+xi+","+xj+" min:"+min+" position:"+ni+","+nj);
	}
}

2.请写一个单例模式的程序。

分析:此处不仅仅是设计一个简单的单例,应该考虑程序的效率,安全性的问题。

代码实现如下:

public class  Test3
{
	public static void main(String[] args) 
	{
		System.out.println(A.getA());
	}
}
class A
{
	private A(){}
	public static A  a=null;;
	public static A getA(){
	if(a==null){
		synchronized(A.class){
			if(a==null){
			a=new A();
			}
		}
	}
		return a;
	  }
}

3.写一段代码,在以下文本中搜索并打印包含单词your(不区分大小写)的句子,并按出现次数由大到小输出

Make me Yourself at home

None of your ourrise business

I will be more careful

How about going to a move

Your life is myour own our affair

分析:要从文本中读取,需要IO流,要匹配字符串要正则,要排序,要记录出现次数可以用键值对形式的Map来装数据。

应该注意熟悉的点:正则的实现,map按键\值排序的实现

代码实现如下:

package net.hncu.d0725;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo1 {
	public static void main(String[] args) {
		File file = new File("1.txt");
		Map<String,Integer> map = new HashMap<String,Integer>();
		Map<String,Integer> sortedMap = new HashMap<String,Integer>();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String buff=null;
			while((buff=reader.readLine())!=null) {
				String key="your";
				int i = pick(buff.toLowerCase(),key);
				if(i>0) {
					map.put(buff,i); 
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sortedMap=sortMapByValue(map);
		for(String s : sortedMap.keySet()) {
			System.out.println(s+" num:"+sortedMap.get(s));
		}
	}
	/**
	 * 按value值给Map排序
	 * @param oriMap
	 * @return
	 */
	public static Map<String,Integer> sortMapByValue(Map<String,Integer> oriMap) {  
	    Map<String,Integer> sortedMap = new LinkedHashMap<String,Integer>();  
	    if (oriMap != null && !oriMap.isEmpty()) {  
	        List<Map.Entry<String,Integer>> entryList = new ArrayList<Map.Entry<String,Integer>>(oriMap.entrySet());  
	        Collections.sort(entryList,new Comparator<Map.Entry<String,Integer>>() {  
	                    public int compare(Entry<String,Integer> entry1,Entry<String,Integer> entry2) {  
	                        int value1 = 0,value2 = 0;  
	                       value1=entry1.getValue();
	                       value2=entry2.getValue();
	                        return value2 - value1;  
	                    }  
	                });  
	        Iterator<Map.Entry<String,Integer>> iter = entryList.iterator();  
	        Map.Entry<String,Integer> tmpEntry = null;  
	        while (iter.hasNext()) {  
	            tmpEntry = iter.next();  
	            sortedMap.put(tmpEntry.getKey(),tmpEntry.getValue());  
	        }  
	    }  
	    return sortedMap;  
	}
	/**
	 * 正则匹配找字符串中的另一个字符串
	 * @param str
	 * @param key
	 * @return
	 */
	public static int pick(String str,String key) {
		int count = 0;
		Pattern p = Pattern.compile(key);
		Matcher m = p.matcher(str);
		while(m.find()) {
			count++;
		}
		return count;
	}

}

4.Java实现统计D:/test ..txt文件出现的数字,汉字,统计个数,汉字,行数,空格。


分析:要读文件要IO,要匹配字符要正则

代码实现:

package net.hncu.d0725;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo2 {
	public static void main(String[] args) {
		File file = new File("resource/2.txt");
		int num=0;
		int letter=0;
		int word=0;
		int line=0;
		int space=0;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String buff=null;
			while((buff=reader.readLine())!=null) {
				num+=select(buff,"num");
				letter+=select(buff,"letter");
				word+=select(buff,"word");
				space+=select(buff,"space");
				line++;
			}
			System.out.printf("num:"+num+"  letter:"+letter+"  word:"+word+"  space:"+space+"  line:"+line);
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static int select(String str,String key) {
		String rule=null;
		switch(key) {
		case "num":
			rule="\\d";
			break;
		case "letter":
			rule="[a-zA-Z]";
			break;
		case "space":
			rule="\\s";
			break;
		case "word":
			rule="[\\u4e00-\\u9fa5]";
			break;
		}
		int count=0;
		Pattern p = Pattern.compile(rule);
		Matcher m = p.matcher(str);
		while(m.find()) {
			count++;
		}
		return count;
	}
}


5.给定四个线程,id分别是aa,bb,cc,dd,请用Java实现循环输出10次aabbccdd。

分析:待定


代码实现:

package net.hncu.d0725;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Demo3 {
	private static int state = 0;

    public static void main(String[] args) {
        final Lock l = new ReentrantLock();
        
        Thread A = new Thread(new Runnable(){
            @Override
            public void run() {
            	int i = 0;
                while (state<=40) {
                    l.lock();
                    if(state%4==0){
                        System.out.print(++i+"AA");
                        state ++;
                    }
                    l.unlock();
                }
            }
        });
        Thread B = new Thread(new Runnable(){
            @Override
            public void run() {
                while (state<=40) {
                    l.lock();
                    if(state%4==1){
                        System.out.print("BB");
                        state ++;
                    }
                    l.unlock();
                }
            }
        });
        Thread C = new Thread(new Runnable(){
            @Override
            public void run() {
                while (state<=40) {
                    l.lock();
                    if(state%4==2){
                        System.out.print("CC");
                        state ++;
                    }
                    l.unlock();
                }
            }
        });
        Thread D = new Thread(new Runnable(){
            @Override
            public void run() {
                while (state<=40) {
                    l.lock();
                    if(state%4==3){
                        System.out.println("DD");
                        state ++;
                    }
                    l.unlock();
                }
            }
        });
        A.start();
        B.start();
        C.start();
        D.start();
    }
}
原文链接:https://www.f2er.com/regex/358153.html

猜你在找的正则表达式相关文章