首页 > 代码库 > 【转载】经典10道c/c++语言经典笔试题(含全部所有参考答案)

【转载】经典10道c/c++语言经典笔试题(含全部所有参考答案)

 

经典10道c/c++语言经典笔试题(含全部所有参考答案)

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 %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是否会导致程序在以后出现问题?为什么?

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

5.(1)写一个函数将一个链表逆序.

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

(3)写一个函数找出一个单向链表的倒数第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.(1)写一个函数,判断一个unsigned char 字符有几位是1.

(2)写一个函数判断计算机的字节存储顺序是升序(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个数不同,写一个搜索算法找出相同的那个数的值.(注意空间效率时间效率尽可能要低).

 

1.

4

80

1600

4 //定义的是一个指向数组的指针

24 //直接对齐

2.

(1)

*(&a[0][0][0]+i)

(2)

编译能通过,就算强制转换也会有问题,a和p所采取的内存布局和寻址方式不同,a的内存假定是连续的,对于a[i][j]可等价于*(&a[0][0]+16*i+j),而char**p不假定使用的是连续的内存,p[i][j]=*(p[i]+j);

3.

非递归方式:

char *reverse(char *str)

{

int len = strlen(str);

char temp;

for(int i=0; i<len/2; i++)

{

temp = *(str+i);

*(str+i) = *(str+len-1-i);

*(str+len-1-i) = temp;

}

return str;

}

递归方式:

char *reverse(char *str)

{

ASSERT(str!=NULL);

char tempstr[1024];

char tempchar ;

memset(tempstr,0,1024);

strcpy(tempstr,str);

int i = strlen(tempstr);

switch(i)

{

case 1:

break;

case 2:

tempchar = str[0];

str[0] = str[1];

str[1] = tempchar;

break;

default:

tempchar=str[0] ;

reverse(str+1);

strcpy(str , str+1);

str[strlen(str)] = tempchar;

break;

}

return str;

}

char *reverse(char *str)

{

assert(str!=NULL);

static char result[256] = "";

if (*str != ‘\0‘)

{

strncat(result, str, 1);

str++;

return(reverse_recursive(str));

}

else

return result;

}

完整程序:

#include <stdio.h>

#include <string.h>

#include <malloc.h>

#include <assert.h>

char *reverse(char *str)

{

assert(str!=NULL);

char *result;

int i, l = strlen(str);

result = (char *)malloc((l+1)*sizeof(char));

for (i=0; i<l; i++)

{

result[i] = str[l-1-i];

}

result[i] = ‘\0‘;

char *reverse_recursive(char *str)

{

assert(str!=NULL);

static char result[256] = "";

if (*str != ‘\0‘)

{

strncat(result, str, 1);

str++;

return(reverse_recursive(str));

}

else

return result;

}

int main()

{

char *rs, *sr = "abcde";

printf("%s\n", sr);

rs = reverse(sr);

printf("%s\n", rs);

free(rs);

rs = reverse_recursive(sr);

printf("%s\n", rs);

4.

strcpy 是复制字符串, 只能操作 char *

memcpy 是内存赋值, 可以复制任何数据。

1)参数类型不同

2)操作方式不同, strcpy 根据参数字符串自动寻找终结位置, memcpy 由参数指定复制长度

5.

(1)

Node * ReverseList(Node *head) //链表逆序

{

if ( head == NULL || head->next == NULL )

return head;

Node *p1 = head ;

Node *p2 = p1->next ;

Node *p3 = p2->next ;

p1->next = NULL ;

while ( p3 != NULL )

{

p2->next = p1 ;

p1 = p2 ;

p2 = p3 ;

p3 = p3->next ;

}

p2->next = p1;

head = p2 ;

return head ;

}

//2)设置2个指针,一个走2步时,另一个走1步。那么一个走到头时,另一个走到中间。

//3)同2),设置2个指针,一个走了n步以后,另一个才开始走。

(2)

node *(List &l)

{

node *p1 = l;

node *p2 = p1;

while( p2 )

{

p2 = p2->next;

if(p2)

{

p2 = p2->next;

p1=p1->next;

}

}

return p1;

}

(3)

node *(List &l,int n)

{

node *next = l;

node *pre = NULL;

while( n-- > 0 && (next != NULL)

{

next = next->next;

}

if(next != NULL)

pre = l;

while(next!=NULL)

{

next = next->next;

pre = pre->next;

}

return pre;

}

6.用递归算法判断数组a[N]是否为一个递增数组。

using namespace std;

bool IsAsc(int*begin,int *end)

{

if(begin == end) return true;

if(*begin>*(begin+1)) return false;

return IsAsc(++begin,end);

}

或:

bool IsIncrease(int *a,int N)

{

return N<=1 || a[0]<=a[1] && IsIncrease(a+1,N-1) ;

}

7.

#include <stdio.h>

#include <stdlib.h>

struct DataLine

{

int no;

char data1[20];

char data2[20];

char name[20];

};

int main()

{

DataLine dl[10];

char data[1024] = "\0";

char temp[20] = "\0";

char *pch,*ptmp;

FILE *fd;

int i = 0;

if ((fd = fopen("a.txt","r")) == NULL)

{

printf("a.txt can not open!\n");

exit -1;

}

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

{

fseek( fd, 0, SEEK_SET );

while(fgets(data, 1024, fd))

{

pch = strchr(data,",");

ptmp = data;

while (pch!=NULL)

{

memset(temp,0x00,sizeof(temp));

//get the data

strncpy(temp,ptmp,pch - ptmp);

//copy the date to dl according the length of temp

if ((strlen(temp) == 1) && ((i + 1) == atoi[temp]))

{

switch (strlen(temp))

{

case 1:

dl[i].no = atoi[temp];

break;

case 8:

strncpy(dl[i].data1,temp,8);

break;

case 4:

strncpy(dl[i].data2,temp,4);

break;

default:

strcpy(dl[i].name,temp);

break;

}

}

ptmp = pch;

pch = strchr(pch+1,‘,‘);

}

}

}

fclose(fd);

if ((fd = fopen("a.txt","w")) == NULL)

{

printf("a.txt can not open!\n");

exit -1;

}

for(i = 0; j < 5; i++)

{

printf("%d\t%s\n",i+1,dl[i].name);

fprintf(fd,"%d,%s,%s,%s,\n",dl[i].no,dl[i].data1,dl[i].data2,dl[i].name);

}

fclose(fd);

return 0;

}

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

int GetBit(unsigned char val)

{

int count=0;

while(val)

{

++count;

val &= (val-1);

}

return count;

}

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

bool IsBigEnd()

{

int i = 1;

return !(*((char*)&i));

}

9.

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[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);

size = j + strlen(s.a);

int m = size + 1;

char* b = new char[j+1];

strcpy(b,a);

a = new char[m];

//strcpy(a,b);

int i = 0;

while(i < j){

a[i] = b[i];

i++;

}

delete[] b;

int k = 0;

while(j < m){

a[j] = s[k];

j++;

k++;

}

a[j] = ‘\0‘;

return *this;

}

int String::length(){

return strlen(a);

}

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;

}

10.有个数组a[100]存放了100个数,这100个数取自1-99,且只有两个相同的数,剩下的98个数不同,写一个搜索算法找出相同的那个数的值.(注意空间效率时间效率尽可能要低).

PS:鉴于以前某人说用int存储数组就是用了O(N)的辅助空间,现在假设是用char存储数组

int GetTheExtraVal(char*var,int size)

{

int val;

int i=0;

for(;i<size;++i)

{

if(var[var[i]&0x7F]&0x80)

{

val=var[i]&0x7F;

break;

}

var[var[i]&0x7F]|=0x80;

}

for(i>=0;--i)

{

var[var[i]&0x7F]&=0x7F;

}

return val;

}

【转载】经典10道c/c++语言经典笔试题(含全部所有参考答案)