论坛首页 招聘求职论坛

一个上机题

浏览 5612 次
锁定老帖子 主题:一个上机题
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-04-02  
输入:一个txt文本文件
输出:字数总数
      排序后再输出
例如:文本的内容是:我爱大海,我爱蓝天
输出:字数总数是:6
       排序后:出现1次:大 海 蓝 天
               出现2次:我 爱
怎么实现呢?
急~~~~~~~~~~~~~~~~~~~~
   发表时间:2009-04-02  
原题是什么?
       排序后:出现1次:大 海 蓝 天
               出现2次:我 爱
是让统计的么?
0 请登录后投票
   发表时间:2009-04-02  
无非就是统计每个字,第一次出现存起来,再出现,计数加1,
最后按出现次数来输出统计信息


两个问题:
1、文件足够大时,内存问题;中间信息存成文件,效率问题
2、文本中的“,”是怎么去掉的?? 符号都不统计吗?中文?英文?
0 请登录后投票
   发表时间:2009-04-03  
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;

/**
* 测试文件读取
* @author Administrator
*
*/
public class FileTest {
public static void main(String[] args){
try {
FileInputStream fis = new FileInputStream("c:/test.txt");
byte[] bt =new byte[1024];
fis.read(bt);
fis.close();
String filestr = new String(bt);
filestr =filestr.trim();
HashMap HZ = new HashMap();
for(int i=0;i<filestr.length();i++){
String temp =filestr.substring(i,i+1);
if(HZ.containsKey(temp)){
int cs =Integer.valueOf(HZ.get(temp).toString());
HZ.put(temp, cs+1);
}else{
HZ.put(temp, 1);
}
}
HZ.toString();
System.out.println(HZ.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
0 请登录后投票
   发表时间:2009-04-03   最后修改:2009-04-03
恩,怎么让我想起了MapReduce,只要是将MR原理的时候就总会引用这个经典case
0 请登录后投票
   发表时间:2009-04-03  
package test.interview;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class TestFileHandler {

	public static void main(String[] args) {
		InputStream input = null;
		try {
			input = new FileInputStream(new File("f:\\test.txt"));
			int size = input.available();
			byte[] content = new byte[size];
			input.read(content);
			String tmp = new String(content, "UTF-8");
			HashMap<Character, Integer> map = new HashMap<Character, Integer>();
			for (int i = 0; i < tmp.length(); i++) {
				if (map.containsKey(tmp.charAt(i))) {
					Integer count = map.get(tmp.charAt(i));
					map.put(tmp.charAt(i), count + 1);
				} else {
					map.put(tmp.charAt(i), 1);
				}
			}

			Set<Character> set = map.keySet();
			Iterator<Character> it = set.iterator();
			while (it.hasNext()) {
				Character word = (Character) it.next();
				System.out.println("word is " + word + " , count is = " + map.get(word));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}



感觉这题完全是考API...
0 请登录后投票
   发表时间:2009-04-03   最后修改:2009-04-03
aquleo 写道
package test.interview;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class TestFileHandler {

	public static void main(String[] args) {
		InputStream input = null;
		try {
			input = new FileInputStream(new File("f:\\test.txt"));
			int size = input.available();
			byte[] content = new byte[size];
			input.read(content);
			String tmp = new String(content, "UTF-8");
			HashMap<Character, Integer> map = new HashMap<Character, Integer>();
			for (int i = 0; i < tmp.length(); i++) {
				if (map.containsKey(tmp.charAt(i))) {
					Integer count = map.get(tmp.charAt(i));
					map.put(tmp.charAt(i), count + 1);
				} else {
					map.put(tmp.charAt(i), 1);
				}
			}

			Set<Character> set = map.keySet();
			Iterator<Character> it = set.iterator();
			while (it.hasNext()) {
				Character word = (Character) it.next();
				System.out.println("word is " + word + " , count is = " + map.get(word));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}



感觉这题完全是考API...

仁者见仁,智者见智
如果用shell作
每个字后加个回车
引用












先排序
引用










正则
引用

爱爱
我我





再正则
引用

2爱
1大
1海
1蓝
1天
2我

排序
.......
循环
引用

2爱我
1大海蓝天
0 请登录后投票
   发表时间:2009-04-10   最后修改:2009-04-10
用正则匹配呢?
我写的。。

public class CountDemo1 {   
    private static final int ONE = 1;   
  
    public static void main(String[] args) throws IOException {   
  
        Map charMap = new TreeMap();   
        StringBuffer sBuffer = new StringBuffer();   
        FileReader reader = new FileReader("c:\\1.txt");   
        BufferedReader br = new BufferedReader(reader);   
        String str = null;   
           
        String regex = "\\w+\\w|[\u4e00-\u9fa5]";   
                   
                while ((str = br.readLine()) != null) {   
            Matcher m = Pattern.compile(regex).matcher(str);   
            while (m.find()) {   
                String charactor = null;   
                charactor = m.group().toLowerCase();   
  
                Integer count = (Integer) charMap.get(charactor);   
  
                if (count == null) {   
                    count = 0;   
                }   
                charMap.put(charactor, ++count);   
            }   
  
        }   
  
        br.close();   
        reader.close();   
  
        List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(   
                charMap.entrySet());   
        Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {   
            public int compare(Map.Entry<String, Integer> o1,   
                    Map.Entry<String, Integer> o2) {   
                return (o2.getValue() - o1.getValue());   
            }   
        });   
        System.out.println("一共有"+infoIds.size()+"個漢字或單詞");   
        for (int i = 0; i < infoIds.size(); i++) {   
            String id = infoIds.get(i).toString();   
            Integer count = infoIds.get(i).getValue();   
            if (count >0 ) {   
                System.out.println(id);   
            }   
  
        }   
  
    }   
}  

0 请登录后投票
   发表时间:2009-04-10   最后修改:2009-04-12
用点面向对象思想:

package com.ib.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestPool {

	public static void main(String[] args) throws FileNotFoundException{
		Scanner scan=new Scanner(new FileInputStream("f:/test.txt"));
		CharacterPool pool=new CharacterPool();
		while(scan.hasNextLine()){
			for(Character c:scan.nextLine().toCharArray()){
				pool.write(c);
			}
		}
		System.out.println("字数总数是:"+pool.getCountSize());
		int i=1;
		do{
			System.out.print("出现"+i+"次:");
			System.out.println(pool.toString());
			pool=pool.getNext();
			i++;
		}while(pool!=null);
	}
}

package com.ib.test;

import java.util.LinkedList;
import java.util.List;

class CharacterPool {

	private List<Character> pool = new LinkedList<Character>();
	private CharacterPool next=null;
	
	public void write(Character c){
		if(c<'\u4e00'||c>'\u9fa5'){
			return;
		}
		if(pool.contains(c)){
			pool.remove(c);
			if(next==null){
				next=new CharacterPool();
			}
			next.write(c);
		}else{
			pool.add(c);
		}
	}
	
	public CharacterPool getNext(){
		return next;
	}
	
	public int getSize(){
		return pool.size();
	}
	
	public int getCountSize(){
		return next==null?pool.size():pool.size()+next.getCountSize();
	}
	
	@Override
	public String toString(){
		return pool.toString();
	}
}

0 请登录后投票
   发表时间:2009-06-04  
哈哈,我出来了
0 请登录后投票
论坛首页 招聘求职版

跳转论坛:
Global site tag (gtag.js) - Google Analytics