您的位置:首页 > 职场人生

IT公司笔试面试题系列(五)

2013-03-15 01:24 211 查看

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中的数字

答案:a[i/6][(i/3)%2][i%3];这道题目是多维数组的输出问题,这里要考虑的是每维数字的取值顺序问题:第一维,前六次循环都取0,后六次取1,于是i/6可以满足要求;第二维,前3次为0,再3次为1,再3次为0,再3次为1,用量化的思想,i/3把12个数字分为4组每组3个,量化为0、1、2、3,为要得到0、1、0、1我们这里就需要对(0、1、2、3)%2=(0、1、0、1),于是(i/3)%2;最后一维我们需要的是(0、1、2;0、1、2;0、1、2;0、1、2;)我们就i%3。

2.char **p, a[16][8]; 
问:p=a是否会导致程序在以后出现问题?为什么?

答案:这个不会导致出现问题,但是要注意p的使用,如a[1][2] 等价的为 *(*(p+1)+2)而不是*(p+11)

3.一个单链表,不知道长度,写一个函数快速找到中间节点的位置.

普通的方法就是先遍历得到链表的长度,然后再通过长度遍历得到链表的中间节点。更好的办法是:

1)  使用两个指针进行遍历,快指针每次步进2,慢指针每次步进1;

2)  当快指针到达链表尾部的时候,慢指针指向的就是链表的中点。

(这个算法的思想和经典问题“判定链表中是否存在环”的思想是一致的)

函数代码如下:

node* find_mid_element(node* head)

{

       if (NULL == head) return NULL;

       if (head->_next == NULL) return head;

       if (head->_next->_next == NULL) return head;

       node* mid= head;

       node* p = mid->_next;

       while ((NULL != p) && (NULL != p->_next))

       {

              mid = mid->_next;

              p = p->_next->_next;

       }

       return mid;

}

下面来道微软的吧

4. 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 <iostream>

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 <cstring>

#include <cstdlib>

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
= '\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 <iostream>

#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 == 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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: