Chinaunix首页 | 论坛 | 博客
  • 博客访问: 152040
  • 博文数量: 64
  • 博客积分: 2545
  • 博客等级: 少校
  • 技术积分: 692
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-22 20:00
文章分类

全部博文(64)

文章存档

2011年(3)

2009年(51)

2008年(10)

我的朋友

分类: C/C++

2009-06-27 13:06:00

1. 下面这段代码的输出是多少(在32位机上).

char *p; // 4

char *q[20]; // 80

char *m[20][20]; // 1600

int (*n)[10]; // 4

struct MyStruct

{

char dda;

double dda1;

int type ;

};
MyStruct k; // 24

printf("%d %d %d %d",sizeof(p),sizeof(q),sizeof(m),sizeof(n),sizeof(k));

2.

(1)

char a[2][2][3]={{{1,6,3},{5,4,15}},{{3,5,33},{23,12,7}} };
for(int i=0;i<12;i++)
printf("%d ",___*(**a+i)____);


在空格处填上合适的语句,顺序打印出a中的数字

(2)

char **p, a[16][8];

问:p=a是否会导致程序在以后出现问题?为什么?

编译就通不过,p是一个指针的指针,而a是一个2维数组的首地址。
但是*p = a也是错误的。

3.用递归方式,非递归方式写函数将一个字符串反转.

函数原型如下:char *reverse(char *str);

#include

/*=======================================================
函 数 名: reverse()
参 数: str
功能描述: 将一个字符串翻转
返 回 值: const char*
抛出异常: 无
作 者: 刘基伟 2007/4/21
=======================================================*/

const char *reverse(char *str);

int main()
{
const char *pch; // 用于取得函数的返回值,来输出翻转后的结果
char chArray[] = " Hello World ! "; // 存储一个将要翻转的字符串
pch = reverse(chArray); // 将字符串chArray翻转
printf("%s\n",pch); // 打印字符串chArray
return 0;
}

const char *reverse(char *str)
{
if(str == NULL)
return NULL;
int nCount = 0; // 用来统计字符串的大小
int nCount_div; // 将字符串的大小折半
const char *pRemark_begin; // 标记字符串的首地址
char chTemp; // 用于交换字符串的临时变量
char *pString_begin; // 存储交换的头指针
char *pString_end; // 存储交换的尾指针

pString_begin = str;
pRemark_begin = str;

while(*str != '\0') // 寻找字符串的结尾
{
str++;
nCount++;
}
pString_end = --str; // 退回一个才是字符串的末尾
nCount_div = nCount/2;

while(nCount_div>0) // 将字符串翻转
{
chTemp = *pString_begin;
*pString_begin = *pString_end;
*pString_end = chTemp;
pString_begin++;
pString_end--;
nCount_div--;
}
return pRemark_begin;
}

4.strcpy函数和memcpy函数有什么区别?它们各自使用时应该注意什么问题?

5.写一个函数将一个链表逆序.
struct Node
{
      int value;
      struct Node *Next;
}

void ReturnReverse(Node *root)
{
      Node *cur=root;
      Node *ptr=root->Next;
      Node * poi=ptr;
      cur->Next=NULL;
      root=root->Next;
      while (ptr!=NULL) {
      poi=ptr;
      ptr=ptr->Next;
      poi->Next=cur->Next;
      cur->Next=poi;  
 }



一个单链表,不知道长度,写一个函数快速找到中间节点的位置.
struct Node
{
      int value;
      struct Node *Next;
}

Node *ReturnMdiNode(Node *root)
{
 Node *ptr1,*ptr2;
 ptr1=root;
 ptr2=root;
 if(root->Next!=NULL)
 {
  while (ptr1->Next!=NULL&&ptr1->Next->Next!=NULL) {
   ptr1=ptr1->Next->Next;
   ptr2=ptr2->Next;
  }
  return ptr2;
 }
 else
  return root;
}



写一个函数找出一个单向链表的倒数第n个节点的指针.(把能想到的最好算法写出).

6.用递归算法判断数组a[N]是否为一个递增数组。
int incrementArray(int *a,int n)
{
if (n<=0)
return 0;
if (n==1)
return 1;
if (a[n-1]>a[n-2])
return incrementArray(a,n
-1);
else
return 0;
}


7.

有一个文件(名为a.txt)如下,每行有4项,第一项是他们的名次,写一个c程序,将五个人的名字打印出来.并按名次排序后将5行数据仍然保存到a.txt中.使文件按名次排列每行.


2,07010188,0711,李镇豪,
1,07010154,0421,陈亦良,
3,07010194,0312,凌瑞松,
4,07010209,0351,罗安祥,
5,07010237,0961,黄世传,

8.写一个函数,判断一个unsigned char 字符有几位是1.

  写一个函数判断计算机的字节存储顺序是升序(little-endian)还是降序(big-endian).

 9.微软的笔试题.

Implement a string class in C++ with basic functionality like comparison, concatenation, input and output. Please also provide some test cases and using scenarios (sample code of using this class).

Please do not use MFC, STL and other libraries in your implementation.

10.有个数组a[100]存放了100个数,这100个数取自1-99,且只有两个相同的数,剩下的98个数不同,写一个搜索算法找出相同的那个数的值.(注意空间效率时间效率尽可能要低).
int ReturnDeplicatedValue(int a[],int n)
{
    int *b=new int[n];
    for(int i=0;i
    {
       if(b[a[i]]!=a[i])
           b[a[i]]=a[i];
       else
           return a[i];
    }
    return -1;
}
void main()
{
    int a[]={1,5,3,6,8,8,7,9,0,4};
    cout<
}

这十道题还是能够看出自己的水平如何的.如果你能不假思索地做出这10道题,估计去国外大公司是没有问题了,呵呵.

答案我在整理中,以后陆续发布.................

下面有些题也不错,可以参考.

1.下面的代码输出是什么,为什么?
       void foo(void)
       {
            unsigned int a = 6;
            int b = -20;
            (a+b>6)?puts(">6"):puts("<=6");//puts为打印函数
       }
输出 >6.

就是考察隐式转换.int型变量转化成unsigned int, b成了正数.

2. b)运行下面的函数会有什么结果?为什么?
       void foo(void)
          {
               char string[10],str1[10];
               int i;
               for(i=0;i<10;i++)
               {
                    str1[i] = 'a';
               }
               strcpy(string, str1);
           printf("%s",string);
          }

首先搞清strcpy函数的实现方法,

char * strcpy(char * strDest,const char * strSrc)
{
  if ((strDest == NULL) || (strSrc == NULL))
    throw "Invalid argument(s)";
  char * strDestCopy = strDest;
  while ((*strDest++ = *strSrc++) != '\0');
  return strDestCopy;
}

由于str1末尾没有‘\0’结束标志,所以strcpy不知道拷贝到何时结束.
printf函数,对于输出char* 类型,顺序打印字符串中的字符直到遇到空字符('\0')或已打印了由精度指定的字符数为止.

下面是微软的两道笔试题....

3. Implement a string class in C++ with basic functionality like comparison, concatenation, input and output. Please also provide some test cases and using scenarios (sample code of using this class).

Please do not use MFC, STL and other libraries in your implementation.

我的实现方案如下,这道题真地对c++的主要特性都进行了较好地考察.

String.h:

#ifndef STRING_H
#define STRING_H

#include
using namespace std;

class String{
   public:
    String();
       String(int n,char c);
    String(const char* source);
    String(const String& s);
    //String& operator=(char* s);
    String& operator=(const String& s);
    ~String();

    char& operator[](int i){return a[i];}
    const char& operator[](int i) const {return a[i];}//对常量的索引.
    String& operator+=(const String& s);
    int length();

   friend istream& operator>>(istream& is, String& s);//搞清为什么将>>设置为友元函数的原因.
   //friend bool operator< (const String& left,const String& right);
   friend bool operator> (const String& left, const String& right);//下面三个运算符都没必要设成友元函数,这里是为了简单.
   friend bool operator== (const String& left, const String& right);
   friend bool operator!= (const String& left, const String& right);
   private:
    char* a;
    int size;
};

#endif


String.cpp:


#include "String.h"
#include
#include

String::String(){
    a = new char[1];
    a[0] = '\0';
    size = 0;
}

String::String(int n,char c){
 a = new char[n + 1];
 memset(a,c,n);
 a[n] = '\0';
 size = n;
}

String::String(const char* source){
 if(source == NULL){
  a = new char[1];
  a[0] = '\0';
  size = 0;
 }
 else
 {   size = strlen(source);
  a = new char[size + 1];
  strcpy(a,source);
 }
}

String::String(const String& s){
 size = strlen(s.a);//可以访问私有变量.
 a = new char[size + 1];
 //if(a == NULL)
 strcpy(a,s.a);
}

 

String& String::operator=(const String& s){
 if(this == &s)
  return *this;
 else
 {
  delete[] a;
        size = strlen(s.a);
  a = new char[size + 1];
  strcpy(a,s.a);
  return *this;
 }
}
String::~String(){
 delete[] a;//   
}

String& String::operator+=(const String& s){
  int j = strlen(a);
  int size = j + strlen(s.a);
  char* tmp = new char[size+1];
  strcpy(tmp,a);
  strcpy(tmp+j,s.a);
 delete[] a;
 a = tmp;

 return *this;
 }

int String::length(){
 return strlen(a);
}

main.cpp:

#include
#include "String.h"

using namespace std;

bool operator==(const String& left, const String& right)
{
 int a = strcmp(left.a,right.a);
    if(a == 0)
  return true;
 else
  return false;
}
bool operator!=(const String& left, const String& right)
{
 return  !(left == right);
}

ostream& operator<<(ostream& os,String& s){
 int length = s.length();
 for(int i = 0;i < length;i++)
  //os << s.a[i];这么不行,私有变量.
  os << s[i];
 return os;
}


String operator+(const String& a,const String& b){
 String temp;
 temp = a;
 temp += b;
 return temp;

}

bool operator<(const String& left,const String& right){
 
 int j = 0;
 while((left[j] != '\0') && (right[j] != '\0')){
  if(left[j] < right[j])
   return true;
  else
  {
   if(left[j] == right[j]){
    j++;
    continue;
   }
   else
    return false;
  }
 }
 if((left[j] == '\0') && (right[j] != '\0'))
  return true;
 else
  return false;
}

bool operator>(const String& left, const String& right)
{   int a = strcmp(left.a,right.a);
    if(a > 0)
  return true;
 else
  return false;
 
}

istream& operator>>(istream& is, String& s){
 delete[] s.a;
 s.a = new char[20];
 int m = 20;
    char c;
 int i = 0;
 while (is.get(c) && isspace(c));
    if (is) {
  do {s.a[i] = c;
       i++;
    /*if(i >= 20){
      cout << "Input too much characters!" << endl;
      exit(-1);
    }*/
    if(i == m - 1 ){
     s.a[i] = '\0';
     char* b = new char[m];
     strcpy(b,s.a);
                 m = m * 2;
        s.a = new char[m];
     strcpy(s.a,b);
     delete[] b;
    }
  }
  while (is.get(c) && !isspace(c));
        //如果读到空白,将其放回.
  if (is)
   is.unget();
 }
 s.size = i;
 s.a[i] = '\0';
 return is;
}


int main(){
 String a = "abcd";
 String b = "www";
 //String c(6,b);这么写不对.
    String c(6,'l');
 String d;
 String e = a;//abcd
 String f;
 cin >> f;//需要输入...
 String g;
 g = a + b;//abcdwww

 if(a < b)
  cout << "a < b" << endl;
 else
  cout << "a >= b" << endl;
 if(e == a)
  cout << "e == a" << endl;
 else
  cout << "e != a" << endl;
 
 b += a;
 
 cout << a << endl;
 cout << b << endl;
    cout << c << endl;
 cout << d << endl;
 cout << e << endl;
 cout << f << endl;
 cout << g << endl;
 cout << g[0] << endl;
 return 0;
}

 

 

4. Implement a single-direction linked list sorting algorithm. Please first define the data structure of linked list and then implement the sorting algorithm.

 

5.编写一个函数,返回两个字符串的最大公串!例如,“adbccadebbca”和“edabccadece”,返回“ccade”

#include "stdafx.h"
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#
char *maxsubstr(char *str1, char *str2)
{
   char *p1, *p2, *q1, *q2, *destp;
   char *substr;
   int max=0, len;

   p1 = str1;
   while(*p1!='\0')
   {
     q1=str2;
     while(*q1!='\0')
     {
       len=0;
       p2=p1;
       q2=q1;
       while((*p2!='\0')&&(*q2!='\0'))
       {
         if(*p2==*q2)
         {
           p2++;q2++;len++;
         }
         else
           break;
       }
       if(len>max)
       {
         max = len;
         destp =p1;
       }
       q1++;
     }
     p1++;
   }
   substr=(char*)malloc(sizeof(char)*max);
   strncpy(substr,destp,max);
   return substr;
 }

 int _tmain(int argc, _TCHAR* argv[])
 {
     char *s1="easderfghjkl";
   char *s2="jasdwfghjewserfghjk";
   char *sub;
   printf("%s\n%s\n",s1,s2);
   sub = maxsubstr(s1,s2);
   printf("the max sub string is:%s",sub);
   return 0;
 }
 

联想笔试题
  1.设计函数 int atoi(char *s)。

 int atoi(const char *nptr);
 函数说明
 atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再 遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。
返回值 返回转换后的整型数。

#include
#include

int myAtoi(const char* s){
 int result = 0;
 int flag = 1;
 int i = 0;
 while(isspace(s[i]))
  i++;
 if(s[i] == '-'){
  flag = -1;
  i++;
 }
 if(s[i] == '+')
  i++;
 while(s[i] != '\0'){
  if((s[i] > '9') || (s[i] < '0'))
   break;
  int j = s[i] - '0';
  result = 10 * result + j;
  i++;
 }
 result = result * flag;
 return result;
}

int main(){
 char* a = "   -1234def";
 char* b = "+1234";
 int i = myAtoi(a);
 int j = myAtoi(b);
 printf("%d \n",i);
 printf("%d",j);
 return 0;
}

 
 



 
  2.int i=(j=4,k=8,l=16,m=32); printf(“%d”, i); 输出是多少?
  3.解释局部变量、全局变量和静态变量的含义。
  4.解释堆和栈的区别。
  5.论述含参数的宏与函数的优缺点。

  普天C++笔试题
  1.实现双向链表删除一个节点P,在节点P后插入一个节点,写出这两个函数。
  2.写一个函数,将其中的\t都转换成4个空格。
  3.Windows程序的入口是哪里?写出Windows消息机制的流程。
  4.如何定义和实现一个类的成员函数为回调函数?
  把函数声明为static
 typedef   void   (*FunPtr)(void);  
  class   A  
  {  
      public   :  
      static   void     callBackFun(void   )  
      {  
          cout<<"callBackFun   "<       }  
  };

  void   Funtype(   FunPtr   p)  
  {  
      p();  
  }  
  void     main(void)  
  {  
  Funtype(A::callBackFun);  
  }
 
  5.C++里面是不是所有的动作都是main()引起的?如果不是,请举例。
并不是所有的动作都是由main()引起的,只是编译器是由main()开始执行的
比如全局对象,它是在main之前就分配好的,全局对象的构造函数在main函数之前运行。
  6.C++里面如何声明const void f(void)函数为C程序中的库函数?
extern "C" void f(void);
  7.下列哪两个是等同的

  int b;
  A const int* a = &b;
  B const* int a = &b;
  C const int* const a = &b;
  D int const* const a = &b;
B和D等同
  8.内联函数在编译时是否做参数类型检查?
  void g(base & b){
   b.play;
  }
  void main(){
   son s;
   g(s);
   return;
  }
华为笔试题
  1.请你分别画出OSI的七层网络结构图和TCP/IP的五层结构图。
  2.请你详细地解释一下IP协议的定义,在哪个层上面?主要有什么作用?TCP与UDP呢?
  3.请问交换机和路由器各自的实现原理是什么?分别在哪个层次上面实现的?
  4.请问C++的类和C里面的struct有什么区别?
  5.请讲一讲析构函数和虚函数的用法和作用。
  6.全局变量和局部变量有什么区别?是怎么实现的?操作系统和编译器是怎么知道的?
  7.8086是多少位的系统?在数据总线上是怎么实现的?
Sony笔试题
  1.完成下列程序
  *
  *.*.
  *..*..*..
  *...*...*...*...
  *....*....*....*....*....
  *.....*.....*.....*.....*.....*.....
  *......*......*......*......*......*......*......
  *.......*.......*.......*.......*.......*.......*.......*.......
  #include
  #define N 8
  int main()
  {
   int i;
   int j;
   int k;
   ---------------------------------------------------------
   | |
   | |
   | |
   ---------------------------------------------------------
   return 0;
  }
  2.完成程序,实现对数组的降序排序
  #include
  void sort( );
  int main()
  {
   int array[]={45,56,76,234,1,34,23,2,3}; //数字任//意给出
   sort( );
   return 0;
  }
  void sort( )
  {
   ____________________________________
   | |
   | |
   |-----------------------------------------------------|
  }
  3.费波那其数列,1,1,2,3,5……编写程序求第十项。可以用递归,也可以用其他方法,但要说明你选择的理由。
  #include
  int Pheponatch(int);
  int main()
  {
   printf("The 10th is %d",Pheponatch(10));
   return 0;
  }
  int Pheponatch(int N)
  {
  --------------------------------
  | |
  | |
  --------------------------------
  }
  4.下列程序运行时会崩溃,请找出错误并改正,并且说明原因。
  #include
  #include
  typedef struct{
   TNode* left;
   TNode* right;
   int value;
  } TNode;
  TNode* root=NULL;
  void append(int N);
  int main()
  {
   append(63);
   append(45);
   append(32);
   append(77);
   append(96);
   append(21);
   append(17); // Again, 数字任意给出
  }
  void append(int N)
  {
   TNode* NewNode=(TNode *)malloc(sizeof(TNode));
   NewNode->value=N;
  
   if(root==NULL)
   {
   root=NewNode;
   return;
   }
   else
   {
   TNode* temp;
   temp=root;
   while((N>=temp.value && temp.left!=NULL) || (N  ))
   {
   while(N>=temp.value && temp.left!=NULL)
   temp=temp.left;
   while(N   temp=temp.right;
   }
   if(N>=temp.value)
   temp.left=NewNode;
   else
   temp.right=NewNode;
   return;
   }
  }

MSRA Interview Written Exam(December 2003,Time:2.5 Hours)


1写出下列算法的时间复杂度。
(1)冒泡排序;
(2)选择排序;
(3)插入排序;
(4)快速排序;
(5)堆排序;
(6)归并排序;

2写出下列程序在X86上的运行结果。

struct mybitfields
{
unsigned short a : 4;
unsigned short b : 5;
unsigned short c : 7;
}test

void main(void)
{
int i;
test.a=2;
test.b=3;
test.c=0;

i=*((short *)&test);
printf("%d\n",i);
}

3写出下列程序的运行结果。

unsigned int i=3;
cout<
4写出下列程序所有可能的运行结果。

int a;
int b;
int c;

void F1()
{
b=a*2;
a=b;
}

void F2()
{
c=a+1;
a=c;
}

main()
{
a=5;
//Start F1,F2 in parallel
F1(); F2();
printf("a=%d\n",a);
}

5考察了一个CharPrev()函数的作用。

6对 16 Bits colors的处理,要求:
(1)Byte转换为RGB时,保留高5、6bits;
(2)RGB转换为Byte时,第2、3位置零。

7一个链表的操作,注意代码的健壮和安全性。要求:
(1)增加一个元素;
(2)获得头元素;
(3)弹出头元素(获得值并删除)。

8一个给定的数值由左边开始升位到右边第N位,如
0010<<1 == 0100
或者
0001 0011<<4 == 0011 0000
请用C或者C++或者其他X86上能运行的程序实现。

附加题(只有在完成以上题目后,才获准回答)
In C++, what does "explicit" mean? what does "protected" mean?

1。在C++中有没有纯虚构造函数?
2。在c++的一个类中声明一个static成员变量有没有用?
3。在C++的一个类中声明一个静态成员函数有没有用?
4。如何实现一个非阻塞的socket?
5。setsockopt, ioctl都可以对socket的属性进行设置,他们有什么不同?
6。解释一下进程和线程的区别?
进程拥有独立的地址空间,进程之间相互独立,一个崩溃不会干扰到其他进程;线程是进程内的一个执行方向,它有自己的局部变量和堆栈,线程之间共享同一进程的地址空间,但一个线程崩溃可造成整个进程崩溃。
所以多进程的程序是安全的,但进程之间切换的开销则很大;线程则通常用于高效率的并发程序。
7。解释一下多播(组播)和广播的含义?
多播只是对特定网段内的计算机发送数据包,一般网络视频会议多采用这种方式;而广播则是对局域网内所有计算机发送数据包,这样容易造成广播风暴。
8。多播采用的协议是什么?
多播是基于UDP协议的方式通讯
9。在c++中纯虚析构函数的作用是什么?请举例说明。
纯虚的析构函数可以定义抽象类,使之不能实例化。因为抽象类中析构函数是没有用的,所以可以把它定义为纯虚,变废为宝。
10。编程,请实现一个c语言中类似atoi的函数功能(输入可能包含非数字和空格)

 原文地址
阅读(490) | 评论(0) | 转发(0) |
0

上一篇:interview C/C++7

下一篇:interview C/C++9

给主人留下些什么吧!~~