论坛首页 入门技术论坛

一道java笔试题

浏览 23439 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-06-11  
你这种写法已经非常的短了,
大约看了一下,应该没什么大的问题
用到了Comparator,估计标准答案应该就是这个了。。。
0 请登录后投票
   发表时间: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");
	}
}



0 请登录后投票
   发表时间:2007-06-11  
Norther 写道
我面试过1家公司也问过这个...不会是同一家吧..汗 附上我的

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




我始终感觉norther的算法功能上好象没有达到吧,所以改进了一下下...主要是可以进行进行完全分组,不过有点小问题,当第一个排序参数排序出来的最后一个和倒数第二个相同的时候会有异常 ,
0 请登录后投票
   发表时间:2007-06-15  
笔试题......
短时间内谁这么牛能做出来
0 请登录后投票
   发表时间: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

这个可以
0 请登录后投票
   发表时间: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
0 请登录后投票
   发表时间:2007-06-15  
楼上回复可真是神速啊!呵呵!题目来也!
我在网上也搜了一下,只发现了一个相关链接,还是只有题目没有答案!说是微软的面试题,是不是真的,当然无从考证!
0 请登录后投票
   发表时间:2007-06-15  
我应聘的职位当然是java软件开发!
0 请登录后投票
   发表时间: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.


网管么
0 请登录后投票
   发表时间:2007-07-03  
可不可以将每一行(如"{12 34 68 32 9 12 545}" )放到对象里,然后对象进行比较,实现类似sql中order by的功能,接下来对行进行排序也就容易多了。
0 请登录后投票
论坛首页 入门技术版

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