经典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. 【Maven实战】传递性依赖的问题

    在上一篇文章中我们已经介绍了依赖性,这次我们再来介绍下传递依赖的问题,首先我们还是在上篇文章基础之上进行编写. 1.上篇文章中已经建立了一个user-core的模块,现在首先再建立一个user-log ...

  2. 理解ThreadLocal(转)

    小结 ThreadLocal是解决线程安全问题一个很好的思路,它通过为每个线程提供一个独立的变量副本解决了变量并发访问的冲突问题.在很多情况下,ThreadLocal比直接使用synchronized ...

  3. app开发历程——android手机显示服务器端图片思路

    以前自己都不知道怎么去显示服务器端的图片,还好在apkbus论坛上找到一个特别简单的例子.虽然一天天忙忙碌碌,但是自己内心其实有一种想逃的心里,说不定哪天就会冒出来. 1.首先服务器端图片 这里的Im ...

  4. Symmetric Tree——LeetCode

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  5. 深入理解object C中复制对象的用法(二)

    第五.对象的自定义拷贝 对象拥有复制特性,必须实现NSCopying,NSMutableCopying协议,实现该协议的copyWithZone方法和mutableCopyWithZone方法 深拷贝 ...

  6. Sublime Text3 配置markdown插件

    sublime是一个亮骚的文本编辑器,而且是跨三大平台,而markdown是一门标记语法,对于记录真是神器,具体语法百度很多,下面教你在sublime上配置markdown. 这两个神器结合起来简直好 ...

  7. 关于Web Api的HelpPage文档注释问题

    之前使用Microsoft.AspNet.WebApi.HelpPage的时候,一直为返回对象的注释发愁,以为这是个BUG. 这个注释的解决办法其实要从其原理理解就明白了. 因为HelpPage是读取 ...

  8. 利用CSS边框合并属性打造table细边框

    CSS代码: <style> table{ border-collapse: collapse;/* 边框合并属性 */ width:200px; } th{ border: 1px so ...

  9. NetworkX学习笔记-5-NetworkX中怎样对多个网络赋属性,并根据属性排序

    这是我在数据分析过程中遇到的实际问题,简单记录一下.这里以DiGraph为例,其他类型的网络(图)的处理方法是一样的. 按照这里:http://networkx.github.io/documenta ...

  10. Nmap扫描教程之DNS服务类

    Nmap扫描教程之DNS服务类 Nmap DNS服务类 DNS(Domain Name System,域名系统)的作用就是将主机名解析为相应IP地址的过程. 通常主机域名的一般结构为:主机名.三级域名 ...