其他公司的笔试题4

来源:互联网 发布:linux ftp配置文件丢失 编辑:程序博客网 时间:2024/04/30 09:29
 
 
 
 
设计线性方程求解程序,打印结果,输入格式为:数字*变量1+数字*变量2.....=0,变量为a-z的小写单个字母。
 
例如:输入3*a+4*x+2=0, 5*a+3*x+4=0,求a=?, x=?
 
完整的程序可能是这样
void RemoveHead(node **head)
{
 if (head == 0 || *head == 0)
 return
 
 node * temp = *head;
 (*head) = (*head)->next;
 free (temp);
}
 
1.数据结构定义:通道和墙组成的迷宫。
用数组定义,1代表墙0代表通道(=_=)
 
2.单链表结构定义:节点存储一个整型数,给代码。合并二个已经按照该整型数从小到大排好序的链表,合并后链表也是同样排序好的。
简单,大概如下,伪算法
Node* uniteList(Node* first,Node*second)
{
Node*head=NULL;
Node*cur=NULL;
Node*temp=NULL;
while (first&&second)
{
if (first->key<second->key)
{
temp=first;
first=first->next;
}
else
{
temp=second;
second=second->next;
}
 
if (head==NULL)
{
cur=temp;
head=cur;
}
else
{
cur->next=temp;
cur=temp
}
 
}
if (first==NULL)
{
temp=second;
}
else
temp=first;
while (temp)
{
cur->next=temp;
temp=temp->next;
}
cur->next=NULL;
return head;
}
 
3.哪些操作会隐式调用C++的拷贝构造函数?
按值返回,按值传递参数,用一个对象初始化另一个对象
4.写出一个函数用来判断一个点到一个平面的关系。用一个点和法向量来表示平面。输入一个点和一个面,返回该点在面的前面,后面,还是在这个面上。
对于第4题:
1.输入:
 一个平面:由平面上的一个点P0和平面的法向量V0表示.
 一个点:PX
2.结果:判断PX和平面的几何关系
3.分析.可以使用向量的相关知识来解决这个问题:
 3.1 计算点P0到PX的向量V = (PX-P0);
 3.2 用点乘计算向量V0和V之间的夹角: doProduct(V,V0) = A;
 3.3 判断关系:
      if (A == 0) ==> 夹角90度 ==> 点在面上
      if (A > 0)   ==> 夹角小于90度 ==> 和法向量一致 ==> 在平面的正面
      if (A < 0)   ==> 夹角大于90度 ==> 和法向量方向相反 ==> 在平面的反面
PS:在图形学中,平面分正反面,法向量所指的面为正面,反之是反面。
 
1、write a program to implement float operation using integer operation on a 32bits processor. c or c++ 
2、write appropriate functions --void itoa(int n,char *s),with pointer,not array indexing,it would be appreciated very much if you could optimize the code and draw a conclusion regarding time complexity as well as space complexity of both algorithms after comparing between the optimized one and the other.
 
3、write a function --invert(x,p,n) --that return x with the n bits that begin at position p inverted(i.e 1 changed into 0 and 0 changed into 1),leaving the others unchanged. 
0001000异或,
4、as far as Intel PXA27X and Linux Kernel V2.4,describe briefly the general ways to optimize the  top-level application based on MiniGUI or QT and give the examples to explain it better.
 
1 What is the fastest way to count the number of ones in a given number represented in a
 
binary form. You can assume that you have infinte run time memory and the response time
 
should be O(1) always. Can you optimize the memory used?
 
 
答案:
 
int bits_set( int word )
{
int tmp;
 
tmp = (word >> 1) & 033333333333;
tmp = word - tmp - ((tmp >> 1) & 033333333333);
return (((tmp + (tmp >> 3)) & 030707070707) % 077);
 
}
2 What will be the complexity of finding the duplicates in an array?
 
3 You are given a stack of punched cards, each with m rows and n columns. Come up with a
 
algorithm to punch holes so that you can find the missing cards (their sequences) in O(1)
 
time. You can assume that you are looking for a missing card and only one is missing out of
all possibilities.
 
4 Write the binary search function in a C language.
二分查找
5 In the given array, find the subsequence with maximum sum and minimum length.
E.g. [10,25,-23,40,-12,39,7] - The subsequence [39,7] has sum of 46 with length 2.
最大子序列
6 Reverse a linked list
反转链表
7 Write an algorithm to compress a text file
压缩
8 How would you implement a BigInt class?
实现大数
9 If you are given x punched cards each with m rows and n
columns, can u come up with a number scheme to identify missing
patterns.
 
10 An absolute number is defined as one in which each digit is
strictly smaller than the digit to its right (if any). For e.g. 123,
478, 369 are all absolute numbers. 205, 485 are not. Write a program
to calculate these.
递增数
1.What is achieved by prefixing the 'static' keyword to a file-level function or file-level variable declaration?
字面理解:使用static修饰词使函数和变量具有文件作用域的目的是什么?
减少名字污染,改变变量的存储区和生存期
 
2.Describe the difference between the “IS A” and “HAS A” object relationships. Which is the stronger relationship and why?
字面理解:stronger应该是指功能更强大
HAS A;IS A在编译期就决定了对象的行为,但是HAS A可以在运行的时候改变"A"所指向的对象,从而可以在运行时改变其行为;HAS A可以有IS A提供的所有特性(至于访问权限,如果需要,可以赋予friend的关系),范例如下
class B
{
 public:
 virtual int Foo();
};
继承:
class D:public B
{
 ......
};
组合:
class D
{
 B*pB;//在这里B可以指向任何一个D的派生类
};
面向对象的设计原则是优先使用组合.
3.Java & C# support interfaces directly with the “interface” keyword.
C++ does not have an “interface” keyword.
How do you create an interface in C++?
Where/when is the use of interfaces especially helpful?
这个是抽象类,应该没什么异议了
 
4.If a program requires a large number of execution contexts what can be done to minimise thread scheduling overhead?
队列,COM里面就是对请求队列进行排队来应对"客户端"的请求的
将这些上下文涉及的局部变量设计成数据结构,然后在线程内部定义一个静态的数据结构,在while(1)循环内部使用数据结构指针来访问上面的数据结构实体.
5. What does it mean to say that a function is reentrant?
What are some of the ways of achieving reentrancy?
1)什么是可重入性?
可重入(reentrant)函数可以由多于一个任务并发使用,而不必担心数据错误。相反,不可重入(non-reentrant)函数不能由超过一个任务所共享,除非能确保函数的互斥(或者使用信号量,或者在代码的关键部分禁用中断)。可重入函数可以在任意时刻被中断,稍后再继续运行,不会丢失数据。可重入函数要么使用本地变量,要么在使用全局变量时保护自己的数据。
 
2)可重入函数:
不为连续的调用持有静态数据。
不返回指向静态数据的指针;所有数据都由函数的调用者提供。
使用本地数据,或者通过制作全局数据的本地拷贝来保护全局数据。
如果必须访问全局变量,记住利用互斥信号量来保护全局变量。
绝不调用任何不可重入函数。;
 
1.用最简单的方法实现函数int strcmp(char *p1,char *p2)
int ret;
//while( *p1 && *p2 && !(ret=*p1-*p2) )
while(!(ret=*p1-*p2) && *p1 && *p2)
{
    p1++;p2++;
}
return ret;
int strcmp(const char * cs,const char * ct)
{
    register signed char __res;
 
    while (1) {
       if ((__res = *cs - *ct++) != 0 || !*cs++)
           break;
    }
 
    return __res;
}
2.a.请定义一个指向此函数的指针 int test(char *p1,int nTestCount)
b.声明此指针的数组,包含10个元素。
c.给数组的第一个元素赋值,使其指向test.
d.写出调用数组的第一个元素,执行test操作实例的语句。
a. int (*func) ( char*, int );
b. func myFunc[10];
c. myFunc[0] = test;
d. char* p;
   int n;
   (*myFunc[0])( p, n );
3.表述应在什么情况下使用extern"C"声明。
void foo( int x, int y )
C编译下会将函数编译为_foo,C++下会编译为_foo_int_int(通常应该是这样)
在调用约定上要特别注意,你的函数名字看是否被改编过了,在调用时应保持与你的声明函数的方
式一样
:
test.h
extern "C" int add( int x, int y);
test.cpp
int add( int x, int y)
{
return x+y;
}
 
调用时:
#include"test.h"
extern int add( int x, int y );
void main()
{
 int sum = add(2,3); //出错,找不到_add_int_int的函数,加载的test.obj里的函数名为_add
 printf( "%d/n", sum );
}
 
改成这样
extern "C"
{
include"tes.h"
}
 
extern int add( int x, int y );
void main()
{
 int sum = add(2,3); //OK
 printf( "%d/n", sum );
}
 
4.对如下双链表
typedef struct _node
{
int iData;
struct _node *pPrev;
struct _node *pNext;
}node;
a.请写出代码,将node*n插入到node*p后。
b.如果多线程同时访问此链表,需要加锁,请说明以下步骤
(a)申请内存给n.
(b)N数据初始化。
(c)插入
注意加锁和解锁的时机。
node* insert(node* p, node* n)
{
    if ((p == NULL) || (n == NULL))
    {
        return NULL;
    }
   
    if (p->pNext != NULL)
    {
        p->pNext->pPrev = n;
    }
   
    n->pPrev = p;
    n->pNext = p->pNext;
    p->pNext = n;
   
    return n;
}
 
b
 
插入之前加锁,插入之后解锁
5.简述C语言中可变长参数的实现原理,如int sprintf(char *buf,const char*format,..)
sprintf(char* pbuf,char* format ...)
      {
 int count ;
 va_list argList;
 va_start(argList,0)
 int type = parser(format);
 
 long x ,y ;
 while( type< TYPECOUNT )
 {
    switch(type)
       {
        case 0:    
       x = va_arg(argList,int);
        break;
       case 1:
       y= va_arg(argList,long);
        break;
       default:
          ...
 }
 va_end(&argList);
}
6.Big endian和Little endian的定义是什么?请用最简单方法(程序实现)判断某个CPU的类型。
big的好像就是低地址存放最高有效字节,little的就是低地址存入最低有效字节(X86)
实现吗就用栈去模拟下就得了吧
7.子类B重写父类A的一个成员函数f,创建一个子类对象C,如果使用C调用父类A的成员函数。
C::A::f();
8.C++中public,protect,private成员函数编译后的可执行代码有何区别?
成员函数被放在可执行文件的不同的节(section)中。
9.对如下定义
typedef struct
{
int iData;
int iAge;
char*pName;
}STUDENT;
STUDENT arrStudent[10];
请写出使用qsort对arrStrudent数组按iAge由小到大的顺序排序。标准函数qsort声明如下:
void qsort(void *base,size_t num,size_t width,int(_cdecl*compare)(const void *elem1,const void *elem2));
qsort(arrStudent, sizeof(arrStudent)/sizeof(STUDENT), sizeof(STUDENT), compare);
 
compare(const void* elem1, const void* elem2)
{
    STUDENT *p1 = reinterpret_cast<STUDENT*>(elem1);
    STUDENT *p2 = reinterpret_cast<STUDENT*>(elem2);
    return p1->iAge - p2->iAge;
}
关于 compare函数,返回<0表示elem1小于elem2,表示相等,>0表示elem1>elem2.
10.对于如下函数
int test(char* p1,int nTestCount)
{
int i,j;
i=nTestCount;...}
画出执行完i=nTestCount后的栈分布状况(你熟悉的CPU和编译器),并说明返回值是如何传递回父函数的。
从栈顶开始,依次是j为随机值,inTestCount的值,p1的值,nTestCount的值
 
编译器会将返回值压入栈中,函数返回后又父函数找到栈位置,并取出该值,然后改变栈顶指针
 
原创粉丝点击