2007-05-31

一道java笔试题

这是某公司的一道java笔试题,题目内容如下:
写一个java程序,实现对一个二维数组按指定的列集进行排序?要求实现类似sql中order by的功能,移动时,整行移动,不能打乱整行顺序。
可将二维数组想象成数据库里的一个表记录集,然后按指定的列集进行排序,即order by col1,col2。
//a为二维数组,sortCols是要按哪几列排序,如0,2,以逗号分隔,升序排。
public void sortIntArrs(int[][] a,String sortCols)
{
}

以下是我回答的程序:

public class MyArray
{
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		int array[][] = new int[][]
		{
		{ 12, 34, 68, 32, 9, 12, 545 },
		{ 34, 72, 82, 57, 56, 0, 213 },
		{ 12, 34, 68, 32, 21, 945, 23 },
		{ 91, 10, 3, 2354, 73, 34, 18 },
		{ 12, 83, 189, 26, 27, 98, 33 },
		{ 47, 23, 889, 24, 899, 23, 657 },
		{ 12, 34, 68, 343, 878, 235, 768 },
		{ 12, 34, 98, 4, 56, 78, 12, 546 },
		{ 26, 78, 2365, 78, 34, 256, 873 } };// 要排序的数组
		int co[] = new int[]
		{ 0, 1, 2, 3, 4 };// 指定进行排序的列索引及排序顺序
		MyArray arraylist = new MyArray();
		arraylist.sortIntArrs(array, co);
	}

	public void sortIntArrs(int[][] a, int[] co)
	{
		for (int i = 0; i < a.length - 1; i++)
		{
			for (int j = i + 1; j < a.length; j++)
			{
				sort(a, co, i, j, 0);
			}
		}
		for (int m = 0; m < a.length; m++)
		{
			for (int n = 0; n < a[m].length; n++)
			{
				System.out.print(a[m][n] + ",");
			}
			System.out.println();
		}
	}

	public void sort(int[][] a, int[] co, int i, int j, int co_index)
	{
		if (co_index < co.length)
		{
			if (a[i][co[co_index]] > a[j][co[co_index]])
			{
				int aa[] = a[i];
				a[i] = a[j];
				a[j] = aa;
			} else if (a[i][co[co_index]] == a[j][co[co_index]])
			{
				sort(a, co, i, j, co_index + 1);
			}
		} else
			return;
	}
}



我的方法中没有将指定的列用字符串参数传递,我采用的是整型数组!
不过我的程序是不正确的,各位有正确的解决方法吗?请赐教!
评论
achunb604 2007-11-18
笔试题怎么可能给你这么长时间去写这么长的代码呢?真服了
java_mid4 2007-11-16
呵呵 我也做了这套题目
不过单链表那道题目他题目上说是o(1) 我想删除链表节点操作应该是o(n)吧
我把做好的题目回过去之后他说我的算法是o(1) 但应该是o(n)
我就晕了 没方向了啊
寂寞秋江 2007-11-16
抛出异常的爱 写道
daoger 写道

1. List troubleshooting steps if your Windows is unable to boot.
2. A Windows PC takes 4 minutes to complete booting. What can be done to reduce the booting time to 1 minute?
3. If a PC is installed Linux, describe steps to install Windows on the same PC and test it.


网管么


呵呵,原来那家公司是 “学海软件”,

前几天我给投他们简历,他们给我回了e-mail,附件里面就是这套题目。我就被吓倒了。

ps:他们好像主要是搞IT方面的视频教程,RIA 方向的吧。
hotfisher 2007-07-03
可不可以将每一行(如"{12 34 68 32 9 12 545}" )放到对象里,然后对象进行比较,实现类似sql中order by的功能,接下来对行进行排序也就容易多了。
抛出异常的爱 2007-06-17
daoger 写道

1. List troubleshooting steps if your Windows is unable to boot.
2. A Windows PC takes 4 minutes to complete booting. What can be done to reduce the booting time to 1 minute?
3. If a PC is installed Linux, describe steps to install Windows on the same PC and test it.


网管么
daoger 2007-06-15
我应聘的职位当然是java软件开发!
daoger 2007-06-15
楼上回复可真是神速啊!呵呵!题目来也!
我在网上也搜了一下,只发现了一个相关链接,还是只有题目没有答案!说是微软的面试题,是不是真的,当然无从考证!
daoger 2007-06-15
今天下午翻邮件,无意中发现前几天一封应聘邮件的回执邮件附件中还有一份笔试题,当初看邮件的时候大意了,打开一看,觉得题目有水平,发上来让各位也做一做!有好的答案还请贴上来分享!

Interview email questions are in 4 categories.

We look for candidates who give good responses in any 2 categories. The quality of answers is important. It is OK to skip some questions. 可以用中文回答.

I) Computer operations (make the answer less than 4 lines for each question)

1. List troubleshooting steps if your Windows is unable to boot.
2. A Windows PC takes 4 minutes to complete booting. What can be done to reduce the booting time to 1 minute?
3. If a PC is installed Linux, describe steps to install Windows on the same PC and test it.

II) Computer science (make the answer less than 4 lines for each question)

4. How to calculate the execution time of a simple instruction “a=3” without writing a program?

III) Programming

5 How to write code to delete a specific node in a single link list (单链表)that takes O(1) time? That is, the time deleting a node is the same (independent from the length of the list.) Link list uses pointers,
not hash. Input is a pointer pointing to the deleted node. Show your algorithm with pseudo code. Hint: just 3 steps.

6 Class A generates random integers between -100 and 100 at a random frequency between 0 and 2 seconds. Class B decrements a counter when class A generated a negative integer and increments when class A generated a positive integer. Class B's counter needs to be updated in real time as class A generates numbers.
a) Show pseudo code in 3 cases: 1) A and B are in the same thread, 2) different threads, c) different processes.
b) Bonus: Show different ways to handling each case.

IV) Design

7. A video software has 3 buttons: Play, Pause, Step Forward. Initially, only play button is visible. When user clicks play button, it starts to play video and only pause button is visible. Click pause, only play and step forward buttons are visible. Complete the state machine table below with 3 states: Play, Pause, Null.
State \ Event Click Play button Click Pause button Click Step Forward button
Init
Play
Pause

8. In a simple game, a ball is moving along a line from the left end toward the right end.
Show UML class diagram
抛出异常的爱 2007-06-15
arjila 写道
笔试题......
短时间内谁这么牛能做出来
huxp 写道
有这么复杂吗?
我写的.好像也可以实现
import java.util.Arrays;
import java.util.Comparator;

public class ArraySort {

	public static void sort(int[][] ob, final int[] order) {
		Arrays.sort(ob, new Comparator() {
			public int compare(Object o1, Object o2) {
				int[] one = (int[]) o1;
				int[] two = (int[]) o2;
				for (int i = 0; i < order.length; i++) {
					int k = order[i];
					if (one[k] > two[k]) {
						return 1;
					} else if (one[k] < two[k]) {
						return -1;
					} else {
						continue;
					}
				}
				return 0;
			}
		});
	}

	public static void main(String[] args) {
		int array[][] = new int[][] { 
				{ 12, 34, 68, 32, 9, 12, 545 }, 
				{ 34, 72, 82, 57, 56, 0, 213 }, 
				{ 12, 34, 68, 32, 21, 945, 23 }, 
				{ 91, 10, 3, 2354, 73, 34, 18 },
				{ 12, 83, 189, 26, 27, 98, 33 }, 
				{ 47, 23, 889, 24, 899, 23, 657 }, 
				{ 12, 34, 68, 343, 878, 235, 768 }, 
				{ 12, 34, 98, 56, 78, 12, 546 }, 
				{ 26, 78, 2365, 78, 34, 256, 873 } };
		sort(array, new int[] { 0,2,3 });
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array[i].length; j++) {
				System.out.print(array[i][j]);
				System.out.print("\t");
			}
			System.out.println();
		}
	}
}

结果:
引用

12 34 68 32 9 12 545
12 34 68 32 21 945 23
12 34 68 343 878 235 768
12 34 98 56 78 12 546
12 83 189 26 27 98 33
26 78 2365 78 34 256 873
34 72 82 57 56 0 213
47 23 889 24 899 23 657
91 10 3 2354 73 34 18

这个可以
arjila 2007-06-15
笔试题......
短时间内谁这么牛能做出来
zkgale 2007-06-11
Norther 写道
我面试过1家公司也问过这个...不会是同一家吧..汗 附上我的

public class SortUtil
{
................................




我始终感觉norther的算法功能上好象没有达到吧,所以改进了一下下...主要是可以进行进行完全分组,不过有点小问题,当第一个排序参数排序出来的最后一个和倒数第二个相同的时候会有异常,
zkgale 2007-06-11
下面是我写的...

希望各位帮忙看看...

package orderby;

public class orderby 
{

	public static void sortIntArrs(int[][] a,String sortCols) 
	{
		int [] colarray=createcolarray(sortCols);
		for(int i=0;i<a.length;i++)
		{
			for(int j=0;j<a[0].length;j++)
			{
				System.out.print(" ["+a[i][j]+"] ");
			}
			System.out.println("");
		}
		System.out.println("*********上面是未排以前,下面是排好了以后********");
		a=paixu(a,colarray);
		for(int i=0;i<a.length;i++)
		{
			for(int j=0;j<a[0].length;j++)
			{
				System.out.print(" ["+a[i][j]+"] ");
			}
			System.out.println("");
		}
	}
	
	public static int[] createcolarray(String sort)//这里是盗用Norther的^^
	{
		try
		{
			String [] so=sort.split(",");
			int [] colarray=new int[so.length];
			for (int i = 0 ; i < so.length ; i++)
			{
				colarray[i]=Integer.parseInt(so[i]);
			}
			return colarray;
		}
		catch (NumberFormatException e)   
        {   
            throw new IllegalArgumentException(   
                    "the argument [" + sort + "] is illegal ,because it must be split by ',' and each element is integer ");   
        }   

		
	}
//排序
	public static int[][] paixu(int[][] a,int[] col)
	{
		paixu2(a,col[0]);		//第一次排
		int[][]x;
		int tj=0;
		//主要功能分次进行排
		for(int k=0;k<col.length-1;k++)	
		{
			for(int i=0;i<a.length-1;i++)//抽出所排的当前列相同的
			{
				if (a[i][col[k]]==a[i+1][col[k]])
				{
					tj++;	//要是有两个的列相同
				}
				else
				{
					if (tj!=0)		//看是否有列相同
					{
						x=new int[tj+1][];
						int mx=0;
						for(int j=i-tj;j<=i;j++)//抽出
						{
							x[mx]=a[j];
							mx++;
						}
						
						x=paixu2(x,col[k+1]);//对下一级进行排
						
						mx=0;
						for(int j=i-tj;j<=i;j++)//还原
						{
							a[j]=x[mx];
							mx++;
						}
					}
					tj=0;
				}
			}
		}
		return a;
	}
	
	//排序
	public static int[][] paixu2(int[][] a,int col)
	{
		//使用冒泡
		for(int i=0;i<a.length-1;i++)
		{
			for(int j=i;j<a.length;j++)
			{
				if (a[i][col]>a[j][col])
				{
					int[] temps=a[i];
					a[i]=a[j];
					a[j]=temps;
				}
			}
		}
		return a;
	}


	public static void main(String [] args)
	{
		int array[][] = new int[][]     
	                                 {     
	                                 { 12, 34, 68, 32, 9, 12, 545 },     
	                                 { 34, 72, 82, 57, 56, 0, 213 },     
	                                 { 12, 34, 68, 32, 21, 945, 23 },     
	                                 { 47, 10, 3, 2354, 73, 34, 18 },     
	                                 { 12, 83, 189, 26, 27, 98, 33 },     
	                                 { 48, 23, 889, 24, 899, 23, 657 },     
	                                 { 12, 34, 68, 343, 878, 235, 768 },     
	                                 { 12, 34, 98, 56, 78, 12, 546 },     
	                                 { 34, 78, 2365, 78, 34, 256, 873 } };   
		sortIntArrs(array,"0,2,3");
	}
}



抛出异常的爱 2007-06-11
你这种写法已经非常的短了,
大约看了一下,应该没什么大的问题
用到了Comparator,估计标准答案应该就是这个了。。。
huxp 2007-06-11
抛出异常的爱 写道
huxp 写道
抛出异常的爱 写道
12, 34, 98, 4, 56, 78, 12, 546 
12, 34, 98, 56, 78, 12, 546 

仔细的看一下这两行。。。。

第一次写法与你的差不多的。。。
不是跟我讲的吧.. 应该不是
你用的例子。。。中的这一行少了一个数字4
列数是不是一样都可以了.只要他包含了此列
而且题目说是类似于数据库里的表.所以列数应该是一样的.
PS: 顺便改了一下代码的一个错误. 现在应该没问题了
抛出异常的爱 2007-06-11
huxp 写道
抛出异常的爱 写道
12, 34, 98, 4, 56, 78, 12, 546 
12, 34, 98, 56, 78, 12, 546 

仔细的看一下这两行。。。。

第一次写法与你的差不多的。。。
不是跟我讲的吧.. 应该不是
你用的例子。。。中的这一行少了一个数字4
huxp 2007-06-11
抛出异常的爱 写道
12, 34, 98, 4, 56, 78, 12, 546 
12, 34, 98, 56, 78, 12, 546 

仔细的看一下这两行。。。。

第一次写法与你的差不多的。。。
不是跟我讲的吧.. 应该不是
抛出异常的爱 2007-06-11
12, 34, 98, 4, 56, 78, 12, 546 
12, 34, 98, 56, 78, 12, 546 

仔细的看一下这两行。。。。

第一次写法与你的差不多的。。。
huxp 2007-06-11
有这么复杂吗?
我写的.好像也可以实现
import java.util.Arrays;
import java.util.Comparator;

public class ArraySort {

	public static void sort(int[][] ob, final int[] order) {
		Arrays.sort(ob, new Comparator() {
			public int compare(Object o1, Object o2) {
				int[] one = (int[]) o1;
				int[] two = (int[]) o2;
				for (int i = 0; i < order.length; i++) {
					int k = order[i];
					if (one[k] > two[k]) {
						return 1;
					} else if (one[k] < two[k]) {
						return -1;
					} else {
						continue;
					}
				}
				return 0;
			}
		});
	}

	public static void main(String[] args) {
		int array[][] = new int[][] { 
				{ 12, 34, 68, 32, 9, 12, 545 }, 
				{ 34, 72, 82, 57, 56, 0, 213 }, 
				{ 12, 34, 68, 32, 21, 945, 23 }, 
				{ 91, 10, 3, 2354, 73, 34, 18 },
				{ 12, 83, 189, 26, 27, 98, 33 }, 
				{ 47, 23, 889, 24, 899, 23, 657 }, 
				{ 12, 34, 68, 343, 878, 235, 768 }, 
				{ 12, 34, 98, 56, 78, 12, 546 }, 
				{ 26, 78, 2365, 78, 34, 256, 873 } };
		sort(array, new int[] { 0,2,3 });
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array[i].length; j++) {
				System.out.print(array[i][j]);
				System.out.print("\t");
			}
			System.out.println();
		}
	}
}

结果:
引用

12 34 68 32 9 12 545
12 34 68 32 21 945 23
12 34 68 343 878 235 768
12 34 98 56 78 12 546
12 83 189 26 27 98 33
26 78 2365 78 34 256 873
34 72 82 57 56 0 213
47 23 889 24 899 23 657
91 10 3 2354 73 34 18
抛出异常的爱 2007-06-06
但测试过没有?
光看代码你的方式与我想的方式差别好大
没看明白。
daoger 2007-06-06
Norther 写道
我面试过1家公司也问过这个...不会是同一家吧..汗 附上我的


呵呵!还真是同一家公司!
daoger
  • 浏览: 183726 次
  • 性别: Icon_minigender_1
  • 来自: 山东济南
  • 详细资料
搜索本博客
我的相册
Fe9b8bf5-5e8b-35c4-ad80-88fec768d45c-thumb
image007
共 105 张
存档
最新评论