读书人

笔考试题求解…

发布时间: 2012-12-19 14:13:14 作者: rapoo

笔试题求解……
今天第一次笔试,两道算法题,跟考官说第二题真不会了,就直接被Pass掉了,说不用面试了,你不适合开发……郁闷到现在

第一题:两个Int数组,都是从小到大的排列,需要合并成一个新的数组,也是从小到大排列,请写出性能比较好的代码。
Fun(int a[m],int b[n])
{
}


第二题:已经有一个包括了多行文本的字符串。请写一个函数,能够输出字符串中最长的行和最短的行。如果最长和最短的不止一行,请输出最长和最短的多行。请写出性能比较好的代码。
Fun(string lines){
1、拆分成多行;
2、循环为每行计算长度,记录最大和最小行的位置;
3、打印输出

}
[最优解释]

引用:
继续努力。只要你愿意,你就行!

稍微修改了一下,这样可能要优雅一点:

static void Main(string[] args)
{
string str = @"aaaa
bbbbb
cccccccccc
dd
dd
eeee";
string[] s = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("行数:" + s.Length);
//if(s.Length==0)return;
List<string> mins = new List<string>();
mins.Add(s[0]);
List<string> maxs = new List<string>();
maxs.Add(s[0]);

for (int i = 1; i < s.Length; i++)
{
if (s[i].Length <= mins[0].Length)
{
if (s[i].Length < mins[0].Length)
mins.Clear();
mins.Add(s[i]);
}

if (s[i].Length >= maxs[0].Length)
{
if (s[i].Length > maxs[0].Length)
maxs.Clear();
maxs.Add(s[i]);
}
}
Console.WriteLine("较小值:");
mins.ForEach(item => Console.WriteLine(item));



Console.WriteLine("较大值:");
maxs.ForEach(item => Console.WriteLine(item));
}


[其他解释]
int[] Fun1(int[] a, int b[])
{
return a.Concat(b).OrderBy(x => x).ToArray();
}

Tuple<string[], string[]> Fun2(string[] lines)
{
var query = lines.GroupBy(x => x.Length).OrderBy(x => x.Key);
return new Tuple<string[], string[]>()
{
Item1 = query.First().ToArray(), //min
Item2 = query.Last().ToArray() //max
};
}

[其他解释]

//
// main.cpp
// acm
//
// Created by BossJue on 12-11-30.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#include <iostream>
using std::endl;
using std::cout;
using std::cin;

int* Merge(int* ArrayA, int iLenA, int* ArrayB, int iLenB);

int main(int argc, const char * argv[])
{

// insert code here...
int A[]= {1,3,5,7};
int B[]= {0,2,4,6,8,10};
int* result = Merge(A, 4, B, 6);
for(int i= 0; i< 10; ++i){
cout<<result[i]<<" ";
}
cout<<endl;
return 0;
}

//ArrayA, ArrayB分别是你要合并的数组,iLenA,iLenB是长度,返回值指向合并之后的数组
int* Merge(int* ArrayA, int iLenA, int* ArrayB, int iLenB){
int* Result= new int[iLenA+ iLenB];
int iItA, iItB, iItAns;
for (iItA= 0, iItB= 0, iItAns= 0; iItA< iLenA && iItB< iLenB;) {
if(ArrayA[iItA]< ArrayB[iItB]){
Result[iItAns++] = ArrayA[iItA++];
}else {
Result[iItAns++] = ArrayB[iItB++];
}
}
while(iItA< iLenA){
Result[iItAns++] = ArrayA[iItA++];
}
while(iItB< iLenB){
Result[iItAns++] = ArrayB[iItB++];
}

return Result;
}



[其他解释]
数组合并,合并之后排序可以使用冒泡法等,关于排序我资源里面有常用的八种排序法及代码,可以去免费下载,下面是合并的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArrayH
{
class Program
{
static void Main(string[] args)
{
int[] intArray = new int[10] { 10, 2, 30, 400, 501 ,6,70,80,9,10};
int[] intArrayOne = new int[] { 1, 2, 3, 4, 5 };
int[] intArrayTwo = new int[] { 6, 7, 8, 9, 10 };
int n = intArrayOne.Length + intArrayTwo.Length;
int[] intArrayThree = new int[n];

int i = 0;
int j = 0;
for (i = 0; i < intArrayThree.Length; i++)
{
if (i < intArrayOne.Length)
{
intArrayThree[i] = intArrayOne[i];
}
else
{
intArrayThree[i] = intArrayTwo[i - intArrayOne.Length];
}
}

Console.WriteLine("合并后的一维数组: ");
foreach (int k in intArrayThree)
Console.Write("{0} ",intArrayThree[k-1].ToString());

int[,] intArrayFour = new int[2, 5];
for (i = 0; i < intArrayFour.GetLength(0); i++)
{
switch (i)
{
case 0:


{
for (j = 0; j < intArrayFour.GetLength(1); j++)
{
intArrayFour[i, j] = intArrayOne[j];
}
}
break;
case 1:
{
for (j = 0; j < intArrayTwo.Length; j++)
{
intArrayFour[i, j] = intArrayTwo[j];
}
}
break;
default:
break;
}
}

Console.WriteLine("合并后的二维数组: ");
for(i = 0;i<intArrayFour.GetLength(0)-2;i++)
{
for (j = 0; j < intArrayFour.GetLength(1)-2; i++)
{
Console.Write(intArrayFour[i, j] + " ");
}


Console.WriteLine();
}

Array.Sort(intArray);
Console.WriteLine();
for (i = 0; i < intArray.Length; i++)
{
Console.Write("{0} ", intArray[i]);
}

Console.WriteLine();
Array.Reverse(intArray);
for (j = 0; j < intArray.Length; j++)
{
Console.Write("{0} ", intArray[j]);
}
Console.ReadKey();
}
}
}

[其他解释]
第一题,我倒是做出来了,已经测试通过。 估计人家也不会要我这么算的。楼主看看吧
int a[4]= {1,3,5,7}; int b[6]= {0,2,4,6,8,10};
int x=0,y=0,z,i,m=4,n=6;
int c[10];

for (z=0;z<m+n;z++)
{
if(a[x]<=b[y])
{
c[z]=a[x];
x++;
if(x>=m)
{
for(i=z+1;i<m+n;i++)
{
c[i]=b[y++];

}
break;
}

}
else
{
c[z]=b[y];
y++;
if(y>=n)
{
for(i=z+1;i<m+n;i++)
{
c[i]=a[x++];

}
break;
}

}
}
[其他解释]
第一题
int[] Fun(int[] a,int[] b)
{
return a.Concat(b).OrderBy(t => t).ToArray();
}
第二题
List<string> Fun(string lines)
{
var ary = Regex.Matches(lines, @"(?m).+").Cast<Match>().Select(t => t.Value).OrderByDescending(t => t.Length);
return ary.Count()<1 ? null : ary.Where(t => t.Length == ary.First().Length).ToList();
}
[其他解释]
第一题就是简单排序 基本都会吧
[其他解释]

引用:
写了几个例子,如果你学过sql查询,我这些内容是很好理解的
#region Linq方式查询

var query = from a in strs


orderby a.Length descending
select a;
……


补上数据:
string[] strs = new[] {
"aaa","aaaa","aaaaa","aaaaaaa","a","aaaaa","aaaaaa","aaaaaaa","a"
};
int[] nums = new[] { 4, 5, 6, 2, 1, -9 };
[其他解释]
int[] a = new int[] { 1,2,3,4,5,6,6,11,12};
int[] b = new int[] { 7, 8, 9, 10 };

List<int> list = new List<int>();
list.AddRange(a);
list.AddRange(b);
list.Sort();
for (int i = 0; i < list.Count-1; i++)
{
Console.WriteLine(list[i].ToString());
}

[其他解释]
引用:
引用:int[] a = new int[] { 1,2,3,4,5,6,6,11,12};
int[] b = new int[] { 7, 8, 9, 10 };

List<int> list = new List<int>();
list.AddRa……

……我猜你为了看输出结果在Console.WriteLine(list[i].ToString()); 后面跟了个Console.Read();之类的东西 还是加在循环里的……这个代码唯一的问题就是for条件……
[其他解释]
引用:
C# code?1234567891011121314int[] Fun1(int[] a, int b[]){ return a.Concat(b).OrderBy(x => x).ToArray();} Tuple<string[], string[]> Fun2(string[] lines){ var query = lines.GroupBy(x =……


return new Tuple<string[], string[]>()
{
Item1 = query.First().ToArray(), //min
Item2 = query.Last().ToArray() //max
};

这样语法通过?? Tuple没有包含0个参数的构造函数。

return Tuple.Create(query.First().ToArray(), query.Last().ToArray());
------其他解决方案--------------------


妹子linq写的溜啊,但是体现不出算法来
第一题两个子序列都是有序的,显然是合并排序那,
第二题用两坨变量放置保存当前最大最小的行信息,逐行扫一遍不断变更那两坨变量
[其他解释]


/*
* 第二题:已经有一个包括了多行文本的字符串。
* 请写一个函数,能够输出字符串中最长的行和最短的行。
* 如果最长和最短的不止一行,请输出最长和最短的多行。
* 请写出性能比较好的代码。
* Fun(string lines)
* {
* 1、拆分成多行;
* 2、循环为每行计算长度,记录最大和最小行的位置;
* 3、打印输出
* }
* */
private static void Function8()
{
//包括了多行文本的字符串 应该是string[] ,而不是string

string[] lines ={ "第二题:已经有一个包括了多行文本的字符串。",
"请写一个函数,能够输出字符串中最长的行和最短的行。",
"如果最长和最短的不止一行,请输出最长和最短的多行。",
"请写出性能比较好的代码。"};
Fun2(lines);
}

private static void Fun2(string[] lines)
{
int maxIndex = int.MinValue;//长度最长的行的长度
int minIndex = int.MaxValue;//长度最短的行的长度

//第一次循环,找出最大的长度和最小的长度
for (int i = 0; i < lines.Length;i++ )
{
int len=lines[i].Length;
if (len>maxIndex)
{
maxIndex = len;
}
if (len<minIndex)
{
minIndex = len;


}
}

//第二次循环,打印符合要求的字符串
for (int i = 0; i < lines.Length; i++)
{
string s=lines[i];
int len = s.Length;
if (len == maxIndex)
{
Console.WriteLine("最长的行:{0}",s);
}
if (len == minIndex)
{
Console.WriteLine("最短的行:{0}", s);
}
}
}


[其他解释]

原来2楼还是女生...是我见过最强悍的女程序员了..
[其他解释]
也可以把行号记录下来,这样能避免再次计算行字符串的长度。
量大的时候,优化是必要的哦~
[其他解释]
引用:
C# code?123456789101112131415161718192021222324 //按题意a,b已经是从小打到的排列 public int[] function1(int[] a, int[] b) { int[] ret = new int[a.Length + b.Length]; ……
补充...
第二题应该在for循环前加

index_max.Add(0);
index_min.Add(0);

[其他解释]
初学的我感觉好难!!!!
[其他解释]

//
// main.cpp
// acm
//
// Created by BossJue on 12-11-30.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#include <iostream>
using std::endl;
using std::cout;
using std::cin;

int* Merge(int* ArrayA, int iLenA, int* ArrayB, int iLenB);

int main(int argc, const char * argv[])
{

// insert code here...
int A[]= {1,3,5,7};
int B[]= {0,2,4,6,8,10};
int* result = Merge(A, 4, B, 6);


for(int i= 0; i< 10; ++i){
cout<<result[i]<<" ";
}
cout<<endl;
return 0;
}

//ArrayA, ArrayB分别是你要合并的数组,iLenA,iLenB是长度,返回值指向合并之后的数组
int* Merge(int* ArrayA, int iLenA, int* ArrayB, int iLenB){
int* Result= new int[iLenA+ iLenB];
int iItA, iItB, iItAns;
for (iItA= 0, iItB= 0, iItAns= 0; iItA< iLenA && iItB< iLenB;) {
if(ArrayA[iItA]< ArrayB[iItB]){
Result[iItAns++] = ArrayA[iItA++];
}else {
Result[iItAns++] = ArrayB[iItB++];
}
}
while(iItA< iLenA){
Result[iItAns++] = ArrayA[iItA++];
}
while(iItB< iLenB){
Result[iItAns++] = ArrayB[iItB++];
}

return Result;
}



[其他解释]

public static void findMinAndMaxLines(String[] lines
, ArrayList<Integer> min_lines, ArrayList<Integer> max_lines) {
int min_len = Integer.MAX_VALUE;
int max_len = Integer.MIN_VALUE;
for (int i=0; i<lines.length; i++) {
if (lines[i].length() < min_len) {
min_len = lines[i].length();
min_lines.clear();
min_lines.add(i);
} else if (lines[i].length() == min_len) {
min_lines.add(i);
}
if (lines[i].length() > max_len) {
max_len = lines[i].length();
max_lines.clear();
max_lines.add(i);
} else if (lines[i].length() == max_len) {


max_lines.add(i);
}
}
}


[其他解释]
第一题是数据结构中的基础题 复杂度大于O(n)的都是基础没学好 从小到大同时扫描两个数组
第二题对字符串的操作,考不到算法了,顶多算类库的使用了。
[其他解释]
好吧 我就看看 女生情有可原 女生本身就不适合做开发(大多数情况下)
[其他解释]
引用:
好吧 我就看看 女生情有可原 女生本身就不适合做开发(大多数情况下)



那你是会还是不会呢?我也不是一点都不会,想法还是有的,就是一下子代码真写不出来啊,我逻辑思维又不差
[其他解释]
引用:
C# code?1234567891011121314int[] Fun1(int[] a, int b[]){ return a.Concat(b).OrderBy(x => x).ToArray();} Tuple<string[], string[]> Fun2(string[] lines){ var query = lines.GroupBy(x =……



额……表示有点看不懂……能稍稍解释下么?
OrderBy(x => x)是怎么个意思?你用的是什么语言?是C#么??
[其他解释]
嘿嘿,对的。
[其他解释]
引用:
嘿嘿,对的。


那OrderBy(x => x)是怎么个意思?
[其他解释]
引用:
引用:嘿嘿,对的。

那OrderBy(x => x)是怎么个意思?

这是LinQ的写法,也叫Lambda表达式,意思是用对象中的那个字段来Order。

给你一个直接程序实现的吧:

static void Main(string[] args)
{
string str = @"aaaa
bbbbb
cccccccccc
dd
dd
eeee";
string[] s = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("行数:" + s.Length);
List<string> mins = new List<string>();
List<string> maxs = new List<string>() { "" };

for (int i = 0; i < s.Length; i++)
{
if (mins.Count == 0)
{
mins.Add(s[i]);
}
else if (s[i].Length < mins[0].Length)


{
mins.Clear();
mins.Add(s[i]);
}
else if (s[i].Length == mins[0].Length)
{
mins.Add(s[i]);
}

if (s[i].Length > maxs[0].Length)
{
maxs.Clear();
maxs.Add(s[i]);
}
else if (s[i].Length == maxs[0].Length)
{
maxs.Add(s[i]);
}
}
Console.WriteLine("较小值:");
mins.ForEach(item => Console.WriteLine(item));

Console.WriteLine("较大值:");
maxs.ForEach(item => Console.WriteLine(item));
}


[其他解释]
继续努力。只要你愿意,你就行!
[其他解释]
引用:
引用:引用:嘿嘿,对的。

那OrderBy(x => x)是怎么个意思?
这是LinQ的写法,也叫Lambda表达式,意思是用对象中的那个字段来Order。

给你一个直接程序实现的吧:
C# code?1234567891011121314151617181920212223242526……


哦~~原来这就是Lambda表达式啊,还没研究过,呵呵
[其他解释]
引用:
继续努力。只要你愿意,你就行!



嗯嗯,谢谢,我会努力了,以后找个不笔试,只面试的,感觉面试比笔试要感觉好
[其他解释]
引用:
引用:引用:引用:嘿嘿,对的。

那OrderBy(x => x)是怎么个意思?
这是LinQ的写法,也叫Lambda表达式,意思是用对象中的那个字段来Order。

给你一个直接程序实现的吧:
C# code?12345678910111213141516……

等你学会了Lambda和LINQ,再去找工作,他们就会问你价钱了,钱低了,轮到你说了,你这个公司不适合我,然后你把他pass了。
------其他解决方案--------------------


看来今后要好好学习LinQ,貌似这个效率挺高的
[其他解释]
LinQ

[其他解释]

引用:
引用:引用:引用:引用:嘿嘿,对的。

那OrderBy(x => x)是怎么个意思?
这是LinQ的写法,也叫Lambda表达式,意思是用对象中的那个字段来Order。

给你一个直接程序实现的吧:
C# code?……


哇塞,可以这么霸气啊,可我现在还很菜,可又要面对实习了,纠结死了都快
[其他解释]
0.0
[其他解释]
引用:
引用:引用:引用:引用:引用:嘿嘿,对的。

那OrderBy(x => x)是怎么个意思?
这是LinQ的写法,也叫Lambda表达式,意思是用对象中的那个字段来Order。

给你一……

不要灰心,面试的说的是你的技术水平不适合开发,而不是你不适合开发,更不是女生不适合开发。只要你把不会的补上去,一样可以的。
[其他解释]
引用:
引用:引用:引用:引用:引用:引用:嘿嘿,对的。

那OrderBy(x => x)是怎么个意思?
这是LinQ的写法,也叫Lambda表达式,意思是……


恩恩,我还不会这么容易放弃的,至少干个一两年再做决定
[其他解释]
这个如何?
[其他解释]
引用:
int[] a = new int[] { 1,2,3,4,5,6,6,11,12};
int[] b = new int[] { 7, 8, 9, 10 };

List<int> list = new List<int>();
list.AddRange(a);
……


你这个最终结果就输出了一个1啊
[其他解释]
至于第二题,其实更简单,事实上最简单的方法就是用正则表达式,这个不解释
非要有C++的话,则从从到尾部扫描一下就可以了
[其他解释]
求妹子关注,呵呵,大爷教你面试
[其他解释]
来学习一下,顺便帮顶!
[其他解释]
是笔试还是机试?


[其他解释]
顺带问一下,笔试是纸上书写呢?还是上机编程呢?
[其他解释]
引用:
恩恩,我还不会这么容易放弃的,至少干个一两年再做决定


一两年编程才刚入门,您打算做什么决定。
[其他解释]
来学习一下,顺便帮顶!
[其他解释]
第一题用Java写了个

/*
* 第一题:两个Int数组,都是从小到大的排列,需要合并成一个新的数组,也是从小到大排列,请写出性能比较好的代码。 Fun(int a[m],int
* b[n]) { }
*/
public int[] fun(int[] a, int[] b) {
int[] c = new int[a.length + b.length];
int ai = 0, bi = 0;//数组下标
while (ai < a.length && bi < b.length)
c[ai + bi] = (a[ai] < b[bi]) ? a[ai++] : b[bi++];


while (ai < a.length)
c[ai + bi] = a[ai++];
while (bi < b.length)
c[ai + bi] = b[bi++];
return c;
}


[其他解释]
我们领导要是面试女生,随便问几个简单问题就过了。而且我们项目组那几个女生都比较轻松,
[其他解释]
很多都是手写的,都考验手写能力。
[其他解释]
引用:
C# code?1234567891011121314int[] Fun1(int[] a, int b[]){ return a.Concat(b).OrderBy(x => x).ToArray();} Tuple<string[], string[]> Fun2(string[] lines){ var query = lines.GroupBy(x =……
你这样写程序完全是显摆。。。。函数式的优点一点都么体现出来。咋一看,好简洁,实际上用的是最笨的方法。
[其他解释]
没什么好担心的,只要努力就好了,女的就不可以当程序员了吗
[其他解释]
第2题中的字符串用split函数分割后,就简化成:在一个整型数组里,求最大/最小值对应数组下标的集合了。
[其他解释]
 
/* 第一题:两个Int数组,都是从小到大的排列,需要合并成一个新的数组,也是从小到大排列,
* 请写出性能比较好的代码。
* Fun(int a[m],int b[n])
* {
* }
*
*/
private static void Function7()
{
int[] a={1,3,5,9,34,90};
int[] b={2,4,6};
int[] c=Fun(a,b);
//数组c即为要计算的结果
}

private static int[] Fun(int[] a, int[] b)
{
int []c=new int [a.Length+b.Length];
int i=0;//c的下标
int j=0;//a的下标
int k=0;//b的下标
do
{
if (a[j]<=b[k])
{
c[i]=a[j];
j++;
i++;


}
else
{
c[i]=b[k];
k++;
i++;
}
} while (j < a.Length && k < b.Length) ;

if (j>=a.Length)//a已空b有剩余
{
while (k < b.Length)
{
c[i] = b[k];
i++;
k++;
}
}
else if (k>=b.Length)//b已空a有剩余
{
while (j < a.Length)
{
c[i] = a[j];
i++;
j++;
}
}
else//a和b同时为空
{

}
return c;
}


[其他解释]
引用:
C# code?1234567891011121314int[] Fun1(int[] a, int b[]){ return a.Concat(b).OrderBy(x => x).ToArray();} Tuple<string[], string[]> Fun2(string[] lines){ var query = lines.GroupBy(x =……
第一题我是用这个方法做的,第二题我没有想出什么好办法,但2楼的第二题写法第一行代码没看懂.
------其他解决方案--------------------



movl-64(%ebp), %eax
addl-60(%ebp), %eax
sall$2, %eax
pushl%eax
.LCFI5:
call_Znaj
addl$16, %esp
movl%eax, -72(%ebp)
pushl-64(%ebp)
pushl-60(%ebp)
leal-56(%ebp), %eax
pushl%eax
leal-24(%ebp), %eax
pushl%eax
call_Z9sortArrayPiS_ii
addl$16, %esp
cmpl$0, -72(%ebp)
je.L3
subl$12, %esp
pushl-72(%ebp)
call_ZdaPv
addl$16, %esp
.L3:
movl$0, %eax
leal-8(%ebp), %esp
popl%esi
popl%edi
leave
ret
.LFE1479:
.Lfe1:
.sizemain,.Lfe1-main
.section.rodata
.LC1:
.string" "
.text
.align 2
.globl _Z9sortArrayPiS_ii
.type_Z9sortArrayPiS_ii,@function
_Z9sortArrayPiS_ii:
.LFB1481:
pushl%ebp
.LCFI6:
movl%esp, %ebp
.LCFI7:
pushl%ebx
.LCFI8:
subl$36, %esp
.LCFI9:
movl16(%ebp), %eax
movl%eax, -8(%ebp)
movl20(%ebp), %eax
movl%eax, -12(%ebp)
subl$12, %esp
movl20(%ebp), %eax
addl16(%ebp), %eax
sall$2, %eax
pushl%eax
.LCFI10:
call_Znaj
addl$16, %esp
movl%eax, -16(%ebp)
movl$0, -20(%ebp)
.L5:
movl-20(%ebp), %eax
cmpl-8(%ebp), %eax
jl.L8
jmp.L6
.L8:
movl-20(%ebp), %eax
leal0(,%eax,4), %ebx
movl-16(%ebp), %ecx
movl-20(%ebp), %eax
leal0(,%eax,4), %edx
movl8(%ebp), %eax
movl(%eax,%edx), %eax
movl%eax, (%ecx,%ebx)
leal-20(%ebp), %eax
incl(%eax)
jmp.L5
.L6:
movl$0, -20(%ebp)
.L9:
movl-20(%ebp), %eax
cmpl-12(%ebp), %eax
jl.L12
jmp.L10
.L12:
movl-20(%ebp), %eax
addl-8(%ebp), %eax
leal0(,%eax,4), %ebx
movl-16(%ebp), %ecx
movl-20(%ebp), %eax
leal0(,%eax,4), %edx
movl12(%ebp), %eax
movl(%eax,%edx), %eax
movl%eax, (%ecx,%ebx)
leal-20(%ebp), %eax
incl(%eax)
jmp.L9
.L10:
movl$0, -20(%ebp)
.L13:
movl-12(%ebp), %eax
addl-8(%ebp), %eax
cmpl%eax, -20(%ebp)
jle.L16
jmp.L14
.L16:
movl$1, -24(%ebp)
.L17:
movl-12(%ebp), %eax
addl-8(%ebp), %eax
decl%eax
cmpl%eax, -24(%ebp)
jl.L20
jmp.L15
.L20:
movl-24(%ebp), %eax
leal0(,%eax,4), %ebx
movl-16(%ebp), %ecx
movl-24(%ebp), %eax
sall$2, %eax
addl-16(%ebp), %eax
leal4(%eax), %edx
movl(%ecx,%ebx), %eax
cmpl(%edx), %eax
jle.L19
movl-24(%ebp), %eax
leal0(,%eax,4), %edx
movl-16(%ebp), %eax
movl(%eax,%edx), %eax
movl%eax, -28(%ebp)
movl-24(%ebp), %eax
leal0(,%eax,4), %edx
movl-16(%ebp), %ecx
movl-24(%ebp), %eax
sall$2, %eax
addl-16(%ebp), %eax
addl$4, %eax
movl(%eax), %eax
movl%eax, (%ecx,%edx)
movl-24(%ebp), %eax
sall$2, %eax
addl-16(%ebp), %eax
leal4(%eax), %edx
movl-28(%ebp), %eax
movl%eax, (%edx)
.L19:
leal-24(%ebp), %eax
incl(%eax)
jmp.L17
.L15:
leal-20(%ebp), %eax
incl(%eax)
jmp.L13
.L14:
movl$0, -20(%ebp)


.L22:
movl-12(%ebp), %eax
addl-8(%ebp), %eax
decl%eax
cmpl%eax, -20(%ebp)
jle.L25
jmp.L23
.L25:
subl$8, %esp
pushl$.LC1
subl$12, %esp
movl-20(%ebp), %eax
leal0(,%eax,4), %edx
movl-16(%ebp), %eax
pushl(%eax,%edx)
pushl$_ZSt4cout
.LCFI11:
call_ZNSolsEi
addl$20, %esp
pushl%eax
.LCFI12:
call_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
addl$16, %esp
leal-20(%ebp), %eax
incl(%eax)
jmp.L22
.L23:
movl$0, %eax
movl-4(%ebp), %ebx
leave
ret
.LFE1481:
.Lfe2:
.size_Z9sortArrayPiS_ii,.Lfe2-_Z9sortArrayPiS_ii
.align 2
.type_Z41__static_initialization_and_destruction_0ii,@function
_Z41__static_initialization_and_destruction_0ii:
.LFB1493:
pushl%ebp
.LCFI13:
movl%esp, %ebp
.LCFI14:
subl$8, %esp
.LCFI15:
cmpl$65535, 12(%ebp)
jne.L26
cmpl$1, 8(%ebp)
jne.L26
subl$12, %esp
pushl$_ZSt8__ioinit
.LCFI16:
call_ZNSt8ios_base4InitC1Ev
addl$16, %esp
subl$4, %esp
pushl$__dso_handle
pushl$0
pushl$__tcf_0
call__cxa_atexit
addl$16, %esp
.L26:
leave
ret
.LFE1493:
.Lfe3:
.size_Z41__static_initialization_and_destruction_0ii,.Lfe3-_Z41__static_initialization_and_destruction_0ii
.align 2
.type__tcf_0,@function
__tcf_0:
.LFB1494:
pushl%ebp
.LCFI17:
movl%esp, %ebp
.LCFI18:
subl$8, %esp
.LCFI19:
subl$12, %esp
pushl$_ZSt8__ioinit
.LCFI20:
call_ZNSt8ios_base4InitD1Ev
addl$16, %esp
leave
ret
.LFE1494:
.Lfe4:
.size__tcf_0,.Lfe4-__tcf_0
.align 2
.type_GLOBAL__I_main,@function
_GLOBAL__I_main:
.LFB1496:
pushl%ebp
.LCFI21:
movl%esp, %ebp
.LCFI22:
subl$8, %esp
.LCFI23:
subl$8, %esp
pushl$65535
pushl$1
.LCFI24:
call_Z41__static_initialization_and_destruction_0ii
addl$16, %esp
leave
ret
.LFE1496:
.Lfe5:
.size_GLOBAL__I_main,.Lfe5-_GLOBAL__I_main
.section.ctors,"aw",@progbits
.align 4
.long_GLOBAL__I_main
.weakpthread_mutex_unlock
.weakpthread_mutex_trylock
.weakpthread_mutex_lock
.weakpthread_create
.weakpthread_setspecific
.weakpthread_getspecific
.weakpthread_key_delete
.weakpthread_key_create
.weakpthread_once
.section.eh_frame,"a",@progbits
.Lframe1:
.long.LECIE1-.LSCIE1
.LSCIE1:
.long0x0
.byte0x1
.string"zP"
.uleb128 0x1
.sleb128 -4
.byte0x8
.uleb128 0x5
.byte0x0
.long__gxx_personality_v0
.byte0xc
.uleb128 0x4
.uleb128 0x4
.byte0x88
.uleb128 0x1
.align 4
.LECIE1:
.LSFDE1:
.long.LEFDE1-.LASFDE1
.LASFDE1:
.long.LASFDE1-.Lframe1
.long.LFB1479
.long.LFE1479-.LFB1479
.uleb128 0x0
.byte0x4
.long.LCFI0-.LFB1479
.byte0xe
.uleb128 0x8
.byte0x85
.uleb128 0x2
.byte0x4
.long.LCFI1-.LCFI0
.byte0xd
.uleb128 0x5
.byte0x4
.long.LCFI4-.LCFI1
.byte0x86
.uleb128 0x4
.byte0x87
.uleb128 0x3
.byte0x4
.long.LCFI5-.LCFI4
.byte0x2e
.uleb128 0x10
.align 4
.LEFDE1:
.LSFDE3:
.long.LEFDE3-.LASFDE3
.LASFDE3:
.long.LASFDE3-.Lframe1


.long.LFB1481
.long.LFE1481-.LFB1481
.uleb128 0x0
.byte0x4
.long.LCFI6-.LFB1481
.byte0xe
.uleb128 0x8
.byte0x85
.uleb128 0x2
.byte0x4
.long.LCFI7-.LCFI6
.byte0xd
.uleb128 0x5
.byte0x4
.long.LCFI9-.LCFI7
.byte0x83
.uleb128 0x3
.byte0x4
.long.LCFI10-.LCFI9
.byte0x2e
.uleb128 0x10
.byte0x4
.long.LCFI11-.LCFI10
.byte0x2e
.uleb128 0x14
.byte0x4
.long.LCFI12-.LCFI11
.byte0x2e
.uleb128 0x10
.align 4
.LEFDE3:
.LSFDE5:
.long.LEFDE5-.LASFDE5
.LASFDE5:
.long.LASFDE5-.Lframe1
.long.LFB1493
.long.LFE1493-.LFB1493
.uleb128 0x0
.byte0x4
.long.LCFI13-.LFB1493
.byte0xe
.uleb128 0x8
.byte0x85
.uleb128 0x2
.byte0x4
.long.LCFI14-.LCFI13
.byte0xd
.uleb128 0x5
.byte0x4
.long.LCFI16-.LCFI14
.byte0x2e
.uleb128 0x10
.align 4
.LEFDE5:
.LSFDE7:
.long.LEFDE7-.LASFDE7
.LASFDE7:
.long.LASFDE7-.Lframe1
.long.LFB1494
.long.LFE1494-.LFB1494
.uleb128 0x0
.byte0x4
.long.LCFI17-.LFB1494
.byte0xe
.uleb128 0x8
.byte0x85
.uleb128 0x2
.byte0x4
.long.LCFI18-.LCFI17
.byte0xd
.uleb128 0x5
.byte0x4
.long.LCFI20-.LCFI18
.byte0x2e
.uleb128 0x10
.align 4
.LEFDE7:
.LSFDE9:
.long.LEFDE9-.LASFDE9
.LASFDE9:
.long.LASFDE9-.Lframe1
.long.LFB1496
.long.LFE1496-.LFB1496
.uleb128 0x0
.byte0x4
.long.LCFI21-.LFB1496
.byte0xe
.uleb128 0x8
.byte0x85
.uleb128 0x2
.byte0x4
.long.LCFI22-.LCFI21
.byte0xd
.uleb128 0x5
.byte0x4
.long.LCFI24-.LCFI22
.byte0x2e
.uleb128 0x10
.align 4
.LEFDE9:
.ident"GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"


[其他解释]
楼主 ,第一题目的分数应该给我,在我后面发言的同学的代码基本跟我的一样,分数必需给我
[其他解释]
引用:
引用:C# code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 /* * 第二题:已经有一个包括了多行文本的字符串。 * 请……
是呀,这也是符合需求的。按照题目的意思,期望结果就是这样的
[其他解释]
 private static void SplitString(string lines) {
// string lines = System.IO.File.ReadAllText("c:\\b.txt",Encoding.UTF8);
string[] strBox = lines.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
SortString(ref strBox, 0, strBox.Length - 1);//先排序
//输出最短字符串
for (int i = 0; i < strBox.Length; i++) {
Console.WriteLine("最短字符串:" + strBox[i]);


if (strBox[i].Length < strBox[i + 1].Length) break;
}

for (int i = strBox.Length - 1; i >= 0; i--)
{
Console.WriteLine("最长字符串:" + strBox[i]);
if (strBox[i].Length > strBox[i-1].Length) break;
}
}
static void SortString(ref string[] strBox, int s, int e)
{
if (s < e) {
int mid = (s + e) / 2;
SortString(ref strBox, s, mid);
SortString(ref strBox, mid + 1, e);
ComboString(ref strBox, s, e, mid);
}
}
static void ComboString(ref string[] strBox, int s, int e, int mid)
{
int i = s, n = e, m = mid, j = mid + 1, k = 0;
string[] temp = new string[e - s + 1];
while (i <= m && j <= n)
{
if (strBox[i].Length > strBox[j].Length)
{
temp[k++] = strBox[j++];
}
else
{
temp[k++] = strBox[i++];
}
}
while (i <= m)


{
temp[k++] = strBox[i++];
}
while (j <= n)
{
temp[k++] = strBox[j++];

}
for (i = 0; i < k; i++)
{
strBox[s + i] = temp[i];
}
}


自己写的第二题
可能有点复杂 其实就是个排序+显示 排序用归并排序 速度还可以
[其他解释]
引用:
C# code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 /* * 第二题:已经有一个包括了多行文本的字符串。 * 请写一个函数,能够输出字符串中最长的行和最……
假如所有行长度都一样,你这个输出不就行数X2了。。。
[其他解释]
引用:
顺带问一下,笔试是纸上书写呢?还是上机编程呢?


纸上写
[其他解释]
引用:
是笔试还是机试?


笔试
[其他解释]
引用:
C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849//// main.cpp// acm//// Created by BossJue on 12-11-30.// Copyright (c) 2012年 __My……



表示有点蒙……
[其他解释]
什么意思?这题目都不全啊.要求是用什么语言实现?需要写伪代码还是真代码
[其他解释]
写了几个例子,如果你学过sql查询,我这些内容是很好理解的
#region Linq方式查询

var query = from a in strs
orderby a.Length descending
select a;
var query2 = from p in nums
orderby p descending
select p;


foreach (var item in query)
{
Console.WriteLine(item);
}
foreach (var item in query2)
{
Console.WriteLine(item.ToString());
}

#endregion

var query3 = strs.OrderByDescending(a => a.Length).Select(a => a);
var max = query3.GroupBy(p => p).First().Select(p => p);
var min = query3.GroupBy(p => p).Last().Select(p => p);
Console.WriteLine("最大数组:");
foreach (var item in max)
{
Console.WriteLine(item);
}
Console.WriteLine("最小数组");
foreach (var item in min)
{
Console.WriteLine(item);
}
Console.Read();
[其他解释]

引用:
引用:引用:顺带问一下,笔试是纸上书写呢?还是上机编程呢?

纸上写


命名空间也要求默写?


[其他解释]
引用:
引用:顺带问一下,笔试是纸上书写呢?还是上机编程呢?

纸上写


[其他解释]

public static int[] combine2SortedArray(int[] a, int[] b) {
int[] c = new int[a.length+b.length];
int i,j,k;
i=j=k=0;
while (i<a.length && j<b.length) {
if (a[i] <= b[j]) {
c[k++] = a[i++];
} else {


c[k++] = b[j++];
}
}
while (i<a.length) {
c[k++] = a[i++];
}
while (j<b.length) {
c[k++] = b[j++];
}
return c;
}


[其他解释]
OH,第二题没有写 显示部分。。
[其他解释]

//按题意a,b已经是从小打到的排列
public int[] function1(int[] a, int[] b)
{
int[] ret = new int[a.Length + b.Length];
int a_index = 0, b_index = 0;
for (int i = 0; i < ret.Length; i++)
{
if (a_index >= a.Length)
{
ret[i] = b[b_index++];
continue;
}
if (b_index >= b.Length)
{
ret[i] = a[a_index++];
continue;
}
if (a[a_index] < b[b_index])
ret[i] = a[a_index++];
else
ret[i] = b[b_index++];
}
return ret;
}


public void function2(string lines)


{
List<int> index_max = new List<int>();
List<int> index_min = new List<int>();
string[] ret = lines.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
int max = ret[0].Length, min = max;
for (int i = 1; i < ret.Length; i++)
{
if (ret[i].Length >= max)
{
if (ret[i].Length != max)
{
max = ret[i].Length;
index_max.Clear();
}
index_max.Add(i);
}

if (ret[i].Length <= min)
{
if (ret[i].Length != min)
{
min = ret[i].Length;
index_min.Clear();
}
index_min.Add(i);
}
}
}


[其他解释]
第二题还是弄不出来,唉!
[其他解释]
string s=你的多行字符串;
s.replace("@",Environment.NewLine);
string[] str=s.split('@');
str[int index]就是你想要的数组;
[其他解释]


我表示楼上的大神们,我已经彻底膜拜了,我只学C#,虽然说汇编,C++,Java都学过,但基本上属于零基础,勉强知道是什么,但看懂还是挺费劲的,呵呵

大神们,你们继续比较着角逐着,我就观望着吧
[其他解释]

引用:
什么意思?这题目都不全啊.要求是用什么语言实现?需要写伪代码还是真代码



用C,C++,C#都行,那人说他只看算法
[其他解释]




//
// main.cpp
// acm
//
// Created by BossJue on 12-11-30.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#include <iostream>
using std::endl;
using std::cout;
using std::cin;

int* Merge(int* ArrayA, int iLenA, int* ArrayB, int iLenB);

int main(int argc, const char * argv[])
{

// insert code here...
int A[]= {1,3,5,7};
int B[]= {0,2,4,6,8,10};
int* result = Merge(A, 4, B, 6);
for(int i= 0; i< 10; ++i){
cout<<result[i]<<" ";
}
cout<<endl;
return 0;
}

//ArrayA, ArrayB分别是你要合并的数组,iLenA,iLenB是长度,返回值指向合并之后的数组
int* Merge(int* ArrayA, int iLenA, int* ArrayB, int iLenB){
int* Result= new int[iLenA+ iLenB];
int iItA, iItB, iItAns;
for (iItA= 0, iItB= 0, iItAns= 0; iItA< iLenA && iItB< iLenB;) {
if(ArrayA[iItA]< ArrayB[iItB]){
Result[iItAns++] = ArrayA[iItA++];
}else {
Result[iItAns++] = ArrayB[iItB++];
}
}
while(iItA< iLenA){
Result[iItAns++] = ArrayA[iItA++];
}
while(iItB< iLenB){
Result[iItAns++] = ArrayB[iItB++];
}

return Result;
}


[其他解释]
用LINQ的。。。。这种情况下。。。面试官考试的是基本功吧!!!!!一上来就写LINQ的。。。
[其他解释]
还有好多不会的。。多学习学习。。
[其他解释]
第一题:两个Int数组,都是从小到大的排列,需要合并成一个新的数组,也是从小到大排列,请写出性能比较好的代码。
Fun(int a[m],int b[n])
{
}

写这个答案的都没注意 传入的参数是从小到大的有序数组。
唉,还用linq去排序,时间复杂度是 a.length+b.length
 class Program
{
static void Main(string[] args)
{


int[] a = { 1, 2, 3, 4, 5, 7 };
int[] b = { 2, 4, 5, 6, 8, 9, 10 };
var result = Fun(a, b);
foreach (int i in result)
{
Console.WriteLine(i);
}
Console.Read();
}

static int[] Fun(int[] a, int[] b)
{
int count = a.Length + b.Length;
int[] newArray = new int[count];

int aIndex = 0;
int bIndex = 0;
for (int i = 0; i < count; i++)
{
if (aIndex < a.Length && a[aIndex] <= b[bIndex])
{
newArray[i] = a[aIndex];
aIndex++;
}
else
{
newArray[i] = b[bIndex];
bIndex++;
}
}

return newArray;
}
}


[其他解释]
引用:
int[] a = new int[] { 1,2,3,4,5,6,6,11,12};
int[] b = new int[] { 7, 8, 9, 10 };

List<int> list = new List<int>();
list.AddRange(a);


……


去反编译看下AddRange的实现
你这么一些复杂度直线上身
另外还多了一次排序操作
其实排序可以直接在合并的一并完成
[其他解释]
引用:
引用:引用:引用:顺带问一下,笔试是纸上书写呢?还是上机编程呢?

纸上写


命名空间也要求默写?



那肯定不用的么……
[其他解释]
引用:
第一题,我倒是做出来了,已经测试通过。 估计人家也不会要我这么算的。楼主看看吧
int a[4]= {1,3,5,7}; int b[6]= {0,2,4,6,8,10};
int x=0,y=0,z,i,m=4,n=6;
int c[10];

for (z=0;z<m+n;z++)
{
if(a[x]<=b[y])
{
……


这个方法性能比较高,利用了数组是由小到大排过序的。时间复杂度是O(N)
[其他解释]
引用:
引用:第一题,我倒是做出来了,已经测试通过。 估计人家也不会要我这么算的。楼主看看吧
int a[4]= {1,3,5,7}; int b[6]= {0,2,4,6,8,10};
int x=0,y=0,z,i,m=4,n=6;
int c[10];

for (z=0;z<m+n;z++)
……



嗯,谢谢,第一题在不考虑程序执行的效率的话,我还是会的,就是第二题,我思路是有的,就是用代码不知道咋写,呵呵
[其他解释]
看大神们谈论的这么激烈,真不知道我还要不要结贴了……
[其他解释]
引用:
今天第一次笔试,两道算法题,跟考官说第二题真不会了,就直接被Pass掉了,说不用面试了,你不适合开发……郁闷到现在

第一题:两个Int数组,都是从小到大的排列,需要合并成一个新的数组,也是从小到大排列,请写出性能比较好的代码。
Fun(int a[m],int b[n])
{
}

时间复杂度O(N+M),每个数组都浏览一遍。

引用:
第二题:已经有一个包括了多行文本的字符串。请写一个函数,能够输出字符串中最长的行和最短的行。如果最长和最短的不止一行,请输出最长和最短的多行。请写出性能比较好的代码。
Fun(string lines){
1、拆分成多行;
2、循环为每行计算长度,记录最大和最小行的位置;
3、打印输出

以空间换时间,分配各与行数数组大小的int性数组,保存每行的长度和偏移量(当然偏移量页可以不保存)。
对数组遍历,第一遍找到最长和最短的大小,第二遍输出。

代码比较简单,就是注意下循环结束条件和输出结束
[其他解释]
-- 没检查到。马上检查
[其他解释]
不行了,再不结贴,分数都不知道咋分了……
[其他解释]
引用:
引用:
引用:第一题,我倒是做出来了,已经测试通过。 估计人家也不会要我这么算的。楼主看看吧
int a[4]= {1,3,5,7}; int b[6]= {0,2,4,6,8,10};
int x=0,y=0,z,i,m=4,n=6;
int c[10];

for (z=0;z<m+n……

第一题的学问也是不少的,我写的还是考虑了很多的。我没写注释,自己看明白就好,程序效率,估计是最高的了。
第二题,我写下思路吧,和注意的要点吧。
已经有一个包括了多行文本的字符串。请写一个函数,能够输出字符串中最长的行和最短的行。如果最长和最短的不止一行,请输出最长和最短的多行。请写出性能比较好的代码。
Fun(string lines){
1、拆分成多行;
循环:
识别第一个\n的位置,将前面的字符存入字符串数组中。
删除\n及其前面的字符,仍然使用原存放多行字符串的变量。我常用memcpy,要用个中间变量。
进行\n的计数。代表行数。
直到不再发现\n或者到串尾。

2、循环
按行数循环
{
依次计算字符串数组的每个长度len[]。
每个串的长度len[],分别与放置最大和最小行号的变量比较,来更新最大和最小行号的变量。
}
3、打印输出
最大和最小行号也就是字符串数组的下标,直接打印对应字符串数组。

------其他解决方案--------------------


我可以给你说下思路,不过我也刚大一,c#还写不出来代码,用c倒是可以,呵呵。
就给你说下思路吧。。
你先读取文本里面一行的数据,遇到换行符就代表一行结束,读取的时候就算出它的长度,然后存长度和和该行的数据。。。继续读取下一行,然后用长度来比较,直到文本结束。。。
不好不要喷啊。
[其他解释]

引用:
用LINQ的。。。。这种情况下。。。面试官考试的是基本功吧!!!!!一上来就写LINQ的。。。



同意!!!
[其他解释]
引用:
引用:第一题:两个Int数组,都是从小到大的排列,需要合并成一个新的数组,也是从小到大排列,请写出性能比较好的代码。
Fun(int a[m],int b[n])
{
}

写这个答案的都没注意 传入的参数是从小到大的有序数组。
唉,还用linq去排序,时间复杂度是 a.length+b.length
C# code?1……


运行了 不越界哈
[其他解释]
引用:
第一题:两个Int数组,都是从小到大的排列,需要合并成一个新的数组,也是从小到大排列,请写出性能比较好的代码。
Fun(int a[m],int b[n])
{
}

写这个答案的都没注意 传入的参数是从小到大的有序数组。
唉,还用linq去排序,时间复杂度是 a.length+b.length
C# code?1234567891011121314151……


如果数组a的长度大于数组b的长度,会出现数组越界吧?
[其他解释]
null
[其他解释]
如果性能没作要求,不需求那么技巧。什么LinQ,Lambda都是浮云!
多行文本,什么是多行文本?有什么标记位?如果是循环,该怎么处理?String有更方便的处理方法吗?

[其他解释]
null
[其他解释]
null
[其他解释]
必须学习了!!
[其他解释]
引用:
C# code?1234567891011121314int[] Fun1(int[] a, int b[]){ return a.Concat(b).OrderBy(x => x).ToArray();} Tuple<string[], string[]> Fun2(string[] lines){ var query = lines.GroupBy(x =……

看来性能最好的还是C#自带的windows工程师写出来的函数。
[其他解释]
第一题是否可以使用arrylist呢
[其他解释]
null

读书人网 >C#

热点推荐