现在的位置: 首页 > 综合 > 正文

趋势笔试题2012(2)

2018年05月05日 ⁄ 综合 ⁄ 共 11182字 ⁄ 字号 评论关闭

from:http://blog.sina.com.cn/s/blog_7ebe664201015yzl.html

1、有一个N*N的棋盘,把有公共边的两个格子叫做相邻的格子。初始时,某些格子里有病毒。每一秒钟,只要一个格子至少有两个相邻格子染上了病毒,那么他自己也会被感染。为了让所有的格子都被感染,初始时最少需要有几个带病毒的格子?给出一种方案并证明最优性。

分析每一次的感染操作之后,被感染格子的总周长不变或减小(新的两条边代替原来两条边,或者原来三条边变成一条新边,或者减少四条边)。因为格子总周长是4n,所以至少要n个;n个是可构造的,对角线就行。因此答案就是n。
******************************************************************************************************************************************
2、小明用6刀,可以把3x3x3的方块切成1x1x1的方块,方块可以随意摆放,问至少要多少刀可以把5x5x5的方块切成1x1x1的方块。

分析:至少要12刀
立方体是三维度,长宽高各切一组,5的要切成1需要4刀,3组就是12刀
nxnxn切1x1x1的算式:3x(n-1)
******************************************************************************************************************************************

3、以下的代码可放在VC++6.0里面运行。题目是要求输出:TrendMicroSoftUSCN 然后要求修改程序,使程序能输出以上结果.代码如下:
#include <iostream>
#include <string>
using namespace std;
int main(int argc,char * argv[])
{
string strArr1[]={ “Trend “, “Micro “, “soft “};
string *p=new string[2];
p[0]= “US “;
p[1]= “CN “;
cout < <sizeof(strArr1) < <endl;
cout < <sizeof(p) < <endl;
cout < <sizeof(string) < <endl;
for(int i=0;i <sizeof(strArr1)/sizeof(string);i++)
cout < <strArr1;
for(i=0;i <sizeof(p)/sizeof(string);i++)
cout < <p;
cout < <endl;
}


答案是:for(i=0;i <sizeof(p)/sizeof(string);i++)
              改为
for(i=0;i <sizeof(*p)*2/sizeof(string);i++)

分析sizeof(strArr1)=48.
          sizeof(string)=16.
          sizeof(p)=4(指针变量)

首先要明确sizeof 不是函数,也不是一元运算符,他是个类似宏定义的特殊关键字,sizeof();括号内在编译过程中是不被编译的,而是被替代类型,如
int a=8;sizeof(a);在编译过程中,它不管a的值是什么,只是被替换成类型 sizeof(int); 结果为4.如果sizeof(a=6);呢,也是一样的转换成a的类型,但是要
注意 因为a=6是不被编译的,所以执行完sizeof(a=6);a的值还是8,是不变的!

记住以下几个结论:
1. unsigned影响的只是最高位bit的意义(正负),数据长度不会被改变的。所以sizeof(unsigned int) ==             sizeof(int);
2. 自定义类型的sizeof取值等同于它的类型原形。如typedef short WORD;sizeof(short) == sizeof(WORD)。
3. 对函数使用sizeof,在编译阶段会被函数返回值的类型取代。如:int f1(){return 0;};
cout < <sizeof(f1()) < <endl; // f1()返回值为int,因此被认为是int
4. 只要是指针,大小就是4。如:cout < <sizeof(string*) < <endl; // 4
5. 数组的大小是各维数的乘积*数组元素的大小。如:

char a[] = “abcdef “;
int b[20] = {3, 4};
char c[2][3] = { “aa “, “bb “};
cout < <sizeof(a) < <endl; // 7
cout < <sizeof(b) < <endl; // 20*4
cout < <sizeof(c) < <endl; // 6
数组a的大小在定义时未指定,编译时给它分配的空间是按照初始化的值确定的,也就是7,包括‘\0’的。
6. 字符串的sizeof和strlen,用例子说明:
char a[] = “abcdef “;
char b[20] = “abcdef “;
string s = “abcdef “;
cout < <strlen(a) < <endl; // 6,字符串长度
cout < <sizeof(a) < <endl; // 7,字符串容量
cout < <strlen(b) < <endl; // 6,字符串长度
cout < <sizeof(b) < <endl; // 20,字符串容量
cout < <sizeof(s) < <endl; // 16, 这里不代表字符串的长度,而是
string类的大小
cout < <strlen(s) < <endl; // 错误!s不是一个字符指针。
a[1] = ‘\0 ‘;
cout < <strlen(a) < <endl; // 1
cout < <sizeof(a) < <endl; // 7,sizeof是恒定的

所以sizeof(p)只是指针大小 为4, 要想求出数组p指向数组的成员个数,应该为sizeof(*p)*2/sizeof(string),为什么?指针p指向数组,则*p就是指向数组中的成员了,成员的类型是什么,string型,ok那么sizeof(*p)为16,乘以2才是整个数组的大小。

******************************************************************************************************************************************

4、There is binary search tree which is used to store characters ‘A’, ‘B’,‘C’,'D’,'E’,'F’,'G’,'H’,which
of the following is post-order tree walk 
result? (有一个二叉搜索树用来存储字符’A', ‘B’, ‘C’,'D’,'E’,'F’,'G’,'H’下面哪个(些)结果是后序树遍历结果)

A. ADBCEGFH
B. BCAGEHFD
C. BCAEFDHG
D. BDACEFHG
E. All of above


分析:
二叉搜索树, 则必满足对树中任一非叶结点, 其左子树都小于该结点值,
右子树所有结点值都大于该结点值
结合二叉树后序遍历的特点, 最后一个肯定是根结点
A. ADBCEGFH  
-> (H) 左子树(ADBCEGF), 右子树(空) (左子树必须都小于根H, 右子树都大于根H)
--> (F) 左子树 (ADBCE), 右子树(G)
---> (E) 左子树 (ADBC), 右子树(空)
----> (C) 剩下(ADB)不能区别左子树, 右子树, 所以选项A不成立
B. BCAGEHFD  
->(D, (BCA), (GEHF))
--> GEHF, F为根, 剩下GEH不能根据F分成两个子段, 所以B不成立
C. BCAEFDHG  
->(G, (BCAEFD), (H))
-->(G, (D, (BCA), (EF)), (H))
--->(G, (D, (A, (), (BC)), (F, (E), ())), (H))
---->(G, (D, (A, (), (C, (B), ())), (F, (E), ())), (H))
          (G)
          / \
        (D) (H)
       /   \
     (A)   (F)
      \    /
      (C)(E)
      /
    (B)
选项(C)成立
D. BDACEFHG  
-> (G, (BDACEF), (H))
--> (G, (F, (BDACE), ()), (H))
---> (G, (F, (E, (BDAC), ()), ()), (H))
----> BDAC子树, C为根, 据C不能将序列BDA划分为两个子序列, 使得左子序列全小于C, 右子序列全大于C
所以选项(D)不成立.
最终答案选C
******************************************************************************************************************************************
5、At time 0,
process A has arrived in the system, in that order; at time 
30, both progress B and C have just arrived; at time 90, both progress
and E have also arrived.The quantum or timeslice is 10 units.

(在0时刻,进程A进入系统,按照这个顺序,在30时刻,进程B和进程C也抵达;在90时刻,进程D和进程E也抵达。一个时间片是10个单元)
Process A requires 50 units of time in the CPU;
Process B requires 40 units of time in the CPU;
Process C requires 30 units of time in the CPU;
Process D requires 20 units of time in the CPU;
Process E requires 10 units of time in the CPU;
进程A需要占用CPU50个单元;
进程B需要占用CPU40个单元;
进程C需要占用CPU30个单元;
进程D需要占用CPU20个单元;
进程E需要占用CPU10个单元;
Which of the process will be the LAST to complete, if scheduling policy
 is
preemptive SJF (Short Job First).please describe the principle.

(如果按照短作业优先级的方法,哪个进程最后结束。请描述原理)

分析: 
A(50) 0-50

C(30) 50-80
B(10) 80-90
E(10) 90-100
D(20) 100-120
B(30) 120-150

**************************************************************************************************
6、Function
club is used to simulate guest in a club. With 0 guests 
initially and 50 as max occupancy, when guests beyond limitation, they need
to wait outside;when some guests leave the waiting list will decrease. The function will print out number of guests in the club and waiting  outside. The function declaration as follows:

void club(int x); positive x stands for guests
arrived, nagative x stands for guests left from within the club

(club函数用来模拟一个俱乐部的顾客。初始化情况下是0个顾客,俱乐部最大规模只能有50个顾客,当用户超过了最大规模,他们必须等在外面。当一些顾客离开了等待队列将减少。这个club函数将打印在俱乐部里面的顾客人数,和外面的等待人数。函数声明如下:
void club(int x);正数x代表客人来了,负数x代表客人离开了俱乐部)
For example, club (40) prints 40,0; and then club (20) prints 50,10; and
then club (-5) prints 50,5; and then club (-30) prints 25,0; and then
club (-30) prints N/A; since it is impossible input.
(举例而言:club (40)打印40,0;接着club (20)打印50,10;接着club (-5)打印50,5;

  接着club (-30)打印25,0;接着club (-30)打印N/A;因为这是不可能实现的。)

Please write the void club(int x) with c++;
To make sure this function works as defined, we have following set of data
to pass into the function and check the result are correct.

(请用c++编程实现club函数。为了确保函数工作正常,我们使用下列数据来测试函数是否正常,你认为该选哪个选项)

a 60
b 20 50 -10
c 40 -30
d 60 -5 -10 -10 10
e 10 -20
f 30 10 10 10 -60
g 10 10 10
h 10 -10 10

A: a d e g
B: c d f g
C: a c d h
D :b d g h
E :c d e f

分析:

跟测试有关,看有没有覆盖所有的边界条件
设A为成员, B为排队
C1 0 0
C2 <50(Up/Down) 0
C3 50 >0(Up/Down)
   
a 60 适用C3 *
b 20 50 -10 适用C2\C3
c 40 -30 适用C2 *
d 60 -5 -10 -10 10 适用C3\C2
e 10 -20 适用C1 *
f 30 10 10 10 -60 适用C1\C2\C3
g 10 10 10 适用C2
h 10 -10 10 适用C2

a e [c|g|h]  
显然e一定要

看了楼上的分析,我也说说,看看条件,第一感觉,肯定要包含e,因为只有他能测N/A的情况,一下就排除了B C D三项,再看A和E,差别在a和c,g和f的选取上,很显然,d包含a,f包含g,所以排除A,最终确定E


**************************************************************************************************

7、The following C++ code tries to count occurence of each ASCII charcater in given string and finally print out the occurrency numbers:(下面C++代码用来统计每个ASCII字符的出现次数,最后给出出现数值)

void histogram(char* src)  
{
    int i;
    char hist[256]; 
    for (i=0;i <256;i++)
    {
        hist[i]=0;
    }
    while(*src!=’\0′)
    {
        hist[*src]++;  //没有自增
 hist[*src++]++;

    }
    for(i=0;i <=256;i++)  //访问越界 for(i=0;i<256;i++)
    {
         printf(“%d\n”, hist[i]);
    }
}
If there may be some issue in the code above, which line(s) would be?
(如果上面代码有错,将在哪行出现?)
A 1 and 3
B 3 and 7
C 9
D 5 and 7
E 4 and 8

分析:
hist[*src]++;
改为
hist[*src++]++;

******************************************************************************************************************************************
8、Consider the
grammar (如下语法)

S–> aSbS|bSaS|V (S是变量)
where V means a terminator for grammar.(V是一个语法的终止符)
which statement(s) below is(are) corrects?(下列哪种说法是正确的)
1. This grammar is not an ambiguous grammar (这个语法不是一个含糊的语法)
2. Sentence ‘abba’ can be constructed by only one parse tree(语句abba能被构造成一个分析树)
3. This grammar construct sentence contain any ‘a’ (1 and more) and any ‘b’ (1 and more)(这个语法构造句      包含一个或多个a和1个或多个b)
4. The grammar equals to regular expression “(a|b)?(a|b)+”(这个语法等于正则表达式(a|b)?(a|b)+)
A :1      B:2 3      C:3      D:1 3      E:2 4


分析:答案B(不确定)
S->aSbS->aSb(bSaS)->aVbbSaS ->aabb  

**************************************************************************************************
9、struct
data
{
unsigned char x;
unsigned short y;
unsigned int z;
}
已知变量var类型为struct data;程序在32位上编,不考虑对齐,请补齐下面汇编语
言,以完成相应C语言功能
var.x=0;
LEA EBX, var
XOR EAX, ______
MOV [EBX], ______
var.y = (unsigned short) (var.z+1)
MOV ECX, ______
INC ECX
MOV ______, ______
var.z = var.y-1;
MOV DX, ______
AND EDX, ______
DEC, EDX
MOV ______, EDX

分析:C/C++
code
#include <iostream>
using namespace std;
struct data 
    unsigned char x; 
    unsigned short y; 
    unsigned int z; 
};

//var.x=0; 
//LEA EBX, var 
//XOR EAX, ______ 
//MOV [EBX], ______ 
//var.y = (unsigned short) (var.z+1) 
//MOV ECX, ______ 
//INC ECX 
//MOV ______, ______ 
//var.z = var.y-1; 
//MOV DX, ______ 
//AND EDX, ______ 
//DEC, EDX 
//MOV ______, EDX 

void dump(struct data& var)
{
    cout << "x=" << (int)var.x << "\t"
         << "y=" << var.y << "\t"
         << "z=" << var.z << endl;
}

int main(int argc, char* argv[])
{
    struct data var = {1,1,1};
    dump(var);
    //var.x = 0;
    __asm{
        lea ebx, var;
        xor eax, eax;
        mov [ebx], eax
    }
    dump(var);
    
    //var.y = (unsigned short) (var.z+1) 
    __asm{
        mov ecx, dword ptr[var.z]
        inc ecx
        mov dword ptr[var.y], ecx
    }
    dump(var);
   
    //var.z = var.y-1; 
    __asm{
        mov edx, var.y
        and edx, 0x0000ffff
        dec edx
        mov dword ptr[var.z], edx
    }
    dump(var);
    return 0;
}
**************************************************************************************************
10、下列程序中能编译通过么?如果不能该如何修改使其通过。
是定义#define VOID_P 1 还是 #defineVOID_P 0,mem才能得到正确地址。

#include  <iostream>
#define VOID_P 0
#if VOID_P
void allocm(void *pout, unsigned long size)
{
    pout = malloc(size);
}
#else
void allocm(void **pout, unsigned long size)
{
    *pout = malloc(size);
}
#elseif

int main(int argc, char *argv[]){
    void *mem = 0;
#if VOID_P
    allocm(mem, 100);
#else
    allocm(&mem, 100);
#endif
    return 0;

}

分析定义#define VOID_P 0

编译通过C/C++代码:

#include <iostream> 

using namespace std; 

#define VOID_P 0

#if VOID_P

void allocm(void *pout, unsigned long size)

{

    pout = malloc(size);

}

#else  

void allocm(void **pout, unsigned long size)

{

    *pout = malloc(size);   

}

#endif

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

    void *mem = 0;

    cout<<mem<<endl;

#if VOID_P

    allocm(mem, 100);

    cout<<mem<<endl;

#else

    allocm(&mem, 100);

    cout<<mem<<endl;

#endif

    return 0;

}

**************************************************************************************************

11、请编C++实现:函数可变长有序数组的插入(无重复数据节点)

int *head = NULL;
int Insert = (int **pHead, int n);

//参数:*pHead 数组首地址 n 插入数值
//返回值: 0成功, 1失败

分析:测试通过C/C++代码:

 #include  <iostream> 

 #include  <cstdlib> 

 #include  <cassert> 

 #include  <cstring> 

 int len_used = 0; 

 int len_totoal = 20; 

 const int inc_per = 20;   

 bool isIn(int **pHead, int n) 

 

   int i; 

   for(i=0; i <len_used && (n > *(*pHead+i)); i++) 

     

   return (n == *(*pHead+i))?true:false;   

   

 int Insert(int **pHead, int n) 

 

     if(isIn(pHead, n)) 

     

         std::cout  << n  <<"already in." << std::endl ; 

         return 1; 

     

     if(len_used >= len_totoal -1) 

     //array is not enough 

     

         len_totoal += inc_per; 

         int *p = (int*)malloc(len_totoal*sizeof(int)); 

         assert(p != NULL); 

         for(int i=0; i  < len_used; i++) 

        { 

            *(p+i) = *(*pHead+i); 

         }  

         free(*pHead); 

         *pHead = p; 

       

     int i,j; 

     //brute sort, you can optimize the program here   

     for(i=0; i <len_used && (n > *(*pHead+i))  ; i++) 

         

     for(j=len_used-1; j>i-1; j--) 

     

         *(*pHead+j+1) = *(*pHead+j); 

     

     *(*pHead+i) = n; 

     len_used += 1; 

     return 0; 

   

 void printArray(int **pHead) 

 

     std::cout  << "array contains:"; 

     for(int i=0; i <len_used; i++) 

     

         std::cout <<*(*pHead+i) <<'\t'; 

     

     std::cout  << std::endl; 

 

 int main() 

  

     int *head = NULL; 

     head = (int*)malloc(len_totoal*sizeof(int)); 

     assert(head != NULL); 

     int **pHead = &head; 

     memset(head, 0, len_totoal);   

     int num; 

     std::cin >> num; 

     while(num != 'q'-'0') 

     

         Insert(pHead, num); 

         printArray(pHead); 

         std::cin >> num; 

     

     free(head); 

     return 0; 

 

抱歉!评论已关闭.