微软面试题汇总

微软面试题汇总

Posted on 2011-09-16 16:04 李大嘴 阅读(214) 评论(1编辑 收藏 

1、有一个整数数组,请求出两两之差绝对值最小的值,记住,只要得出最小值即可,不需要求出是哪两个数。

View Code
复制代码
  1 using System;
2 using System.Linq;
3 using System.Collections.Generic;
4 namespace ConsoleApplication1
5 {
6 class Program
7 {
8 static Random Rand =new Random();
9
10 staticvoid Main(string[] args)
11 {
12 int count =10000;
13 List<int> Input =new List<int>();
14 // for (int i = 0; i < count; i++)
15 // {
16 // Input.Add(Rand.Next(int.MinValue, int.MaxValue));
17 // }
18 Input.Add(1); Input.Add(2); Input.Add(3); Input.Add(4); Input.Add(5); Input.Add(19);
19
20 ulong re = PigeonNest(Input, ulong.MaxValue);
21 Console.WriteLine(re);
22 Console.WriteLine(ulong.MaxValue);
23 Console.WriteLine(Input.Min());
24 }
25
26 //鸽巢原理。
27 staticulong PigeonNest(List<int> List, ulong MinResult)
28 {
29 switch (List.Count)
30 {
31 case0:
32 case1:
33 return MinResult;
34 case2:
35 return ABS(List[0], List[1]);
36 default:
37 break;
38 }
39 int min = List.Min();
40 //确定桶的大小。
41 int width = (int)Math.Ceiling((double)(List.Max() - min) / List.Count);
42 //不可能比1还小了。
43 if (width ==1) { return1ul; }
44 //把数据丢到桶里。
45 Dictionary<int, NumbersInfo> EachNestNum =new Dictionary<int, NumbersInfo>();
46 foreach (int n in List)
47 {
48 int Key = Convert.ToInt32(Math.Ceiling((double)(n - min) / width));
49 if (!EachNestNum.ContainsKey(Key))
50 {
51 EachNestNum.Add(Key, new NumbersInfo(Key));
52 }
53 EachNestNum[Key].Add(n);
54 }
55 //找到所有桶里,和相邻两桶的最大最小值距离,三个数中最近的。
56 foreach (int Key in EachNestNum.Keys)
57 {
58 MinResult = Min(MinResult, EachNestNum[Key].minresult(EachNestNum, MinResult));
59 }
60 return MinResult;
61 }
62 class NumbersInfo
63 {
64 public NumbersInfo(int k)
65 { key = k; }
66 private List<int> List =new List<int>();
67 privateint key;
68 publicint max =int.MinValue;
69 publicint min =int.MaxValue;
70 publicint count { get { return List.Count; } }
71 publiculong minresult(Dictionary<int, NumbersInfo> EachNestNum, ulong re)
72 {
73 //在三个数中选最小的。
74 //当命中数大于1的时候,递归这个过程。由于迅速收敛,故复杂度忽略不计。
75 if (List.Count >1)
76 {
77 re = PigeonNest(List, re);
78 }
79 if (EachNestNum.ContainsKey(key -1))
80 {
81 re = Min(ABS(EachNestNum[key].min, EachNestNum[key -1].max), re);
82 }
83 if (EachNestNum.ContainsKey(key +1))
84 {
85 re = Min(ABS(EachNestNum[key].max, EachNestNum[key +1].min), re);
86 }
87 return re;
88 }
89 publicvoid Add(int x)
90 {
91 List.Add(x);
92 if (x > max) { max = x; }
93 if (x < min) { min = x; }
94 }
95 }
96
97
98 staticulong ABS(int x, int y)
99 {
100 //三分。
101 switch (x.CompareTo(y))
102 {
103 case-1:
104 return (ulong)y - (ulong)x;
105 case1:
106 return (ulong)x - (ulong)y;
107 }
108 return0ul;
109
110 }
111
112 staticulong Min(ulong x, ulong y)
113 {
114 if (x > y) { return y; }
115 return x;
116 }
117 }
118 }
复制代码

2、平面上N个点,没两个点都确定一条直线,求出斜率最大的那条直线所通过的两个点

平面上N个点,没两个点都确定一条直线,求出斜率最大的那条直线所通过的两个点(斜率不存在的情况不考虑)。时间效率越高越好。
先把N个点按x排序。
斜率k最大值为max(斜率(point[i],point[i+1]))    0 <=i <n-2。
复杂度Nlog(N)。
以3个点为例,按照x排序后为ABC,假如3点共线,则斜率一样,假如不共线,则可以证明AB或BC中,一定有一个点的斜率大于AC,一个点的斜率小于AC。

3、写一个函数,检查字符是否是整数,如果是,返回其整数值。(或者:怎样只用4行代码编写出一个从字符串到长整型的函数)

View Code

4、怎样编写一个程序,把一个有序整数数组放到二叉树中?

View Code

5、怎样从顶部开始逐层打印二叉树结点数据?请编程。

View Code

6、编程实现两个正整数的除法

View Code

7、在排序数组中,找出给定数字的出现次数。比如 [1, 2, 2, 2, 3] 中2的出现次数是3次。

View Code

8、一个整数数列,元素取值可能是0~65535中的任意一个数,相同数值不会重复出现。0是例外,可以反复出现。
请设计一个算法,当你从该数列中随意选取5个数值,判断这5个数值是否连续相邻。
注意:
- 5个数值允许是乱序的。比如: 8 7 5 0 6
- 0可以通配任意数值。比如:8 7 5 0 6 中的0可以通配成9或者4
- 0可以多次出现。
- 复杂度如果是O(n2)则不得分。

 

插入排序-》最大值-最小值<=4

9、一棵排序二叉树,令 f=(最大值+最小值)/2,设计一个算法,找出距离f值最近、大于f值的结点。复杂度如果是O(n2)则不得分。

View Code

10、

 
分类: 求职应试
posted @ 2012-10-03 16:08  ppshinebl  阅读(164)  评论(0编辑  收藏  举报