公司笔试c、c++试题
现在的公司招聘,都要笔试⾯试.如果你不是那种编程功底⾮常深厚的⼈,⼜不好好准备⼀番,在笔试⾯试中往往会处于被动局⾯.虽然有些笔试题是故意为难我们,有点钻⽜⾓尖.但是很多笔试题⾯试题确实能够很好地看出我们的基础.
在这⾥,我就略去那些钻⽜⾓尖的题.从csdn论坛我近半年的收集中选出10道有代表性的题⽬,难度基本上是逐渐加⼤.对数组,指针,数据结构,算法,字符串,⽂件操作等问题都有覆盖.主要以c语⾔的实现为主,也有c++的题.⼤家可以先做做这10道题,测试⼀下⾃⼰的⽔平.
1. 下⾯这段代码的输出是多少(在32位机上).
char *p;
char *q[20];
char *m[20][20];
int (*n)[10];
struct MyStruct
{
char dda;
double dda1;
int type ;
};
MyStruct k;
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中的数字
(2)
char **p, a[16][8];
问:p=a是否会导致程序在以后出现问题?为什么?
3.⽤递归⽅式,⾮递归⽅式写函数将⼀个字符串反转.
函数原型如下:char *reverse(char *str);
4.strcpy函数和memcpy函数有什么区别?它们各⾃使⽤时应该注意什么问题?
5.写⼀个函数将⼀个链表逆序.
⼀个单链表,不知道长度,写⼀个函数快速到中间节点的位置.
写⼀个函数出⼀个单向链表的倒数第n个节点的指针.(把能想到的最好算法写出).
6.⽤递归算法判断数组a[N]是否为⼀个递增数组。
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个数不同,写⼀个搜索算法出相同的那个数的值. (注意空间效率时间效率尽可能要低).
这⼗道题还是能够看出⾃⼰的⽔平如何的.如果你能不假思索地做出这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 <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>
字符串拷贝函数strcpy作用#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[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 <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;
}