经典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++语言经典笔试题(含全部所有参考答案)的更多相关文章

  1. 10道C++输出易错笔试题收集

    下面这些题目都是我之前准备笔试面试过程中积累的,大部分都是知名公司的笔试题,C++基础薄弱的很容易栽进去.我从中选了10道简单的题,C++初学者可以进来挑战下,C++大牛也可以作为娱乐玩下(比如下面的 ...

  2. 10道不得不会的JavaEE面试题

    10道不得不会的 JavaEE 面试题 我是 JavaPub,专注于面试.副业,技术人的成长记录. 以下是 JavaEE 面试题,相信大家都会有种及眼熟又陌生的感觉.看过可能在短暂的面试后又马上忘记了 ...

  3. iOS进阶面试题----经典10道

    OneV‘s Den在博客里出了10道iOS面试题,用他的话是:"列出了十个应聘Leader级别的高级Cocoa/CocoaTouch开发工程师所应该掌握和理解的技术" .  在这 ...

  4. C语言基础笔试题一

    1.下面的代码输出什么?为什么? void foo(void) { unsigned int a = 6; int b = -20; (a+b > 6)?puts(">6&quo ...

  5. [转] C语言常见笔试题大全1

    点击阅读原文 1. 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题) #define SECONDS_PER_YEAR (60 * 60 * 24 * 365UL) [ ...

  6. 考研计算机复试(C语言复试笔试题)(精华题选)

    1.以下选择中,正确的赋值语句是(C) A.a=1,b=2 B.j++ C.a=b=5; D.y=(int)x 解析:选项A.B.D都无分号 变量 = 表达式;//赋值语句是一定带分号的 int a= ...

  7. 华为C语言笔试题集合

    ①华为笔试题搜集 1.static有什么用途?(请至少说明两种)     1)在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变.     2) 在模块内(但在函数体外),一个被声明为 ...

  8. C语言超级经典400道题目

    C语言超级经典400道题目 1.C语言程序的基本单位是____ A) 程序行 B) 语句 C) 函数 D) 字符.C.1 2.C语言程序的三种基本结构是____构A.顺序结构,选择结构,循环结 B.递 ...

  9. C语言经典算法100例(一)

    C语言中有有许多经典的算法,这些算法都是许多人的智慧结晶,也是编程中常用的算法,这里面包含了众多算法思想,掌握这些算法,对于学习更高级的.更难的算法都会有很大的帮助,会为自己的算法学习打下坚实的基础. ...

随机推荐

  1. js方法在iframe父子窗口

    http://developer.51cto.com/art/201009/228891.htm http://developer.51cto.com/art/201009/228891.htm ht ...

  2. j2ee爬坑行之一:web容器

    什么是容器? servlet没用main方法,它们受控于另一个java应用程序,这个应用程序就称为容器. tomcat就是这样一个容器.当web服务器得到一个指向某servlet的请求,此时服务器不是 ...

  3. http://aws.amazon.com/cn/ses/ 亚马逊 营销性非事务邮件发送平台

    http://aws.amazon.com/cn/ses/   亚马逊 营销性非事务邮件发送平台

  4. SWFObject: 基于Javascript的Flash媒体版本检测与嵌入模块

    原文地址:http://www.awflasher.com/flash/articles/swfobj.htm SWFObject: 基于Javascript的Flash媒体版本检测与嵌入模块原文:S ...

  5. Linux文件操作学习总结【转载】

    本文转载自: http://blog.csdn.net/xiaoweibeibei/article/details/6556951 文件类型:普通文件(文本文件,二进制文件).目录文件.链接文件.设备 ...

  6. 最长回文子串(百度笔试题和hdu 3068)

    版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/17123559 求一个字符串的最长回文子串.注 ...

  7. yii 删除内容时增加ajax提示

    环境 : 后台有新闻分类和新闻的文章,在分类下有文章存在的时候,不想用户删除分类 代码 controller public function actionDelete($id) { $data = C ...

  8. C语言原码反码补码与位运算.

      目录:     一.机器数和真值     二.原码,反码和补码的基础概念     三.为什么要使用原码,反码和补码     四.原码,补码,反码再深入     五.数据溢出测试     六.位运算 ...

  9. NYOJ-745蚂蚁的难题(二)

    这道题和求字段和的要求就差一点,就是那个是一条链, 这个是个环,关于这么环,刚开始按照链那种方式推倒状态转移方程,但是没有写出来,后来看题解,才看到原来还是转化为普通的单链来做,好多题都是由不会的转化 ...

  10. Highcharts 设置折线图的显示与隐藏

    1.初始化隐藏某条曲线 series 配置如: [ {name:"发帖", postCountData}, {name:"删帖帖", deleteCountDa ...