资源描述
CC++笔试题5
资料仅供参考
4. static有什么用途?(请至少说明两种)
1.限制变量的作用域
2.设置变量的存储域
7. 引用与指针有什么区别?
1) 引用必须被初始化,指针不必。
2) 引用初始化以后不能被改变,指针能够改变所指的对象。
2) 不存在指向空值的引用,可是存在指向空值的指针。
8. 描述实时系统的基本特性
在特定时间内完成特定的任务,实时性与可靠性
9. 全局变量和局部变量在内存中是否有区别?如果有,是什么区别?
全局变量储存在静态数据库,局部变量在堆栈
10. 什么是平衡二叉树?
左右子树都是平衡二叉树 且左右子树的深度差值的绝对值不大于1
11. 堆栈溢出一般是由什么原因导致的?
没有回收垃圾资源
12. 什么函数不能声明为虚函数?
constructor
13. 冒泡排序算法的时间复杂度是什么?
O(n^2)
14. 写出float x 与“零值”比较的if语句。
if(x>0.000001&&x<-0.000001)
16. Internet采用哪种网络协议?该协议的主要层次结构?
tcp/ip 应用层/传输层/网络层/数据链路层/物理层
17. Internet物理地址和IP地址转换采用什么协议?
ARP (Address Resolution Protocol)(地址解析協議)
18.IP地址的编码分为哪俩部分?
IP地址由两部分组成,网络号和主机号。不过是要和“子网掩码”按位与上之后才能区分哪些是网络位哪些是主机位。
2.用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。写出C程序。
循环链表,用取余操作做
3.不能做switch()的参数类型是:
switch的参数不能为实型。
写一段程序,找出数组中第k大小的数,输出数所在的位置。例如{2,4,3,4,7}中,第一大的数是7,位置在4。第二大、第三大的数都是4,位置在1、3随便输出哪一个均可。函数接口为:int find_orderk(const int* narry,const int n,const int k)
要求算法复杂度不能是O(n^2)
谢谢!
能够先用快速排序进行排序,其中用另外一个进行地址查找
代码如下,在VC++6.0运行经过。给分吧^-^
//快速排序
#include<iostream>
usingnamespacestd;
intPartition (int*L,intlow,int high)
{
inttemp = L[low];
intpt = L[low];
while (low < high)
{
while (low < high && L[high] >= pt)
--high;
L[low] = L[high];
while (low < high && L[low] <= pt)
++low;
L[low] = temp;
}
L[low] = temp;
returnlow;
}
voidQSort (int*L,intlow,int high)
{
if (low < high)
{
intpl = Partition (L,low,high);
QSort (L,low,pl - 1);
QSort (L,pl + 1,high);
}
}
intmain ()
{
intnarry[100],addr[100];
intsum = 1,t;
cout << "Input number:" << endl;
cin >> t;
while (t != -1)
{
narry[sum] = t;
addr[sum - 1] = t;
sum++;
cin >> t;
}
sum -= 1;
QSort (narry,1,sum);
for (int i = 1; i <= sum;i++)
cout << narry[ i] << '\t';
cout << endl;
intk;
cout << "Please input place you want:" << endl;
cin >> k;
intaa = 1;
intkk = 0;
for (;;)
{
if (aa == k)
break;
if (narry[kk] != narry[kk + 1])
{
aa += 1;
kk++;
}
}
cout << "The NO." << k << "number is:" << narry[sum - kk] << endl;
cout << "And it's place is:" ;
for (i = 0;i < sum;i++)
{
if (addr[ i] == narry[sum - kk])
cout << i << '\t';
}
return0;
}
1、找错
Void test1()
{
char string[10];
char* str1="";
strcpy(string, str1);// 溢出,应该包括一个存放'\0'的字符string[11]
}
Void test2()
{
char string[10], str1[10];
for(I=0; I<10;I++)
{
str1[ i] ='a';
}
strcpy(string, str1);// I,i没有声明。str1[9]=0;
}
Void test3(char* str1)
{
char string[10];
if(strlen(str1)<=10)// 改成<10,字符溢出。不能将strlen改为sizeof。
{
strcpy(string, str1);
}
}
2.
void g(int**);
int main()
{
int line[10],i;
int *p=line; //p是数组首地址
for (i=0;i<10;i++)
{
*p=i;
g(&p);//数组对应的值加1
}
for(i=0;i<10;i++)
printf("%d\n",line[ i]);
return 0;
}
void g(int**p)
{
(**p)++;
(*p)++;// 指向数组下一个元素
}
输出:
1
2
3
4
5
6
7
8
9
10
3. 写出程序运行结果
int sum(int a)
{
auto int c=0;
static int b=3;
c+=1;
b+=2;
return(a+b+c);
}
void main()
{
int I;
int a=2;
for(I=0;I<5;I++)
{
printf("%d,", sum(a));
}
}
// static会保存上次结果,记住这一点,剩下的自己写
输出:8,10,12,14,16,
4.
int func(int a)
{
int b;
switch(a)
{
case 1: 30;
case 2: 20;
case 3: 16;
default: 0
}
return b;
}
则func(1)=?
// b定义后就没有赋值。
5:
int a[3];
a[0]=0; a[1]=1; a[2]=2;
int *p, *q;
p=a;
q=&a[2];
则a[q-p]=a[2]
解释:指针一次移动一个int但计数为1
今天早上的面试题9道,比较难,向牛人请教,国内的一牛公司,坐落在北京北四环某大厦:
1、线形表a、b为两个有序升序的线形表,编写一程序,使两个有序线形表合并成一个有序升序线形表h;
答案在 请化大学 严锐敏《数据结构第二版》第二章例题,数据结构当中,这个叫做:两路归并排序
Linklist *unio(Linklist *p,Linklist *q){
linklist *R,*pa,*qa,*ra;
pa=p;
qa=q;
R=ra=p;
while(pa->next!=NULL&&qa->next!=NULL){
if(pa->data>qa->data){
ra->next=qa;
qa=qa->next;
}
else{
ra->next=pa;
pa=pa->next;
}
}
if(pa->next!=NULL)
ra->next=pa;
if(qa->next!=NULL)
ra->next==qa;
return R;
}
2、运用四色定理,为N个局域举行配色,颜色为1、2、3、4四种,另有数组adj[][N],如adj[ i][j]=1则表示i区域与j区域相邻,数组color[N],如color[ i]=1,表示i区域的颜色为1号颜色。
四色填充
3、用递归算法判断数组a[N]是否为一个递增数组。
递归的方法,记录当前最大的,而且判断当前的是否比这个还大,大则继续,否则返回false结束:
bool fun( int a[], int n )
{
if( n= =1 )
return true;
if( n= =2 )
return a[n-1] >= a[n-2];
return fun( a,n-1) && ( a[n-1] >= a[n-2] );
}
4、编写算法,从10亿个浮点数当中,选出其中最大的10000个。
用外部排序,在《数据结构》书上有
《计算方法导论》在找到第n大的数的算法上加工
5、编写一unix程序,防止僵尸进程的出现.
同学的4道面试题,应聘的职位是搜索引擎工程师,后两道超级难,(希望大家多给一些算发)
1.给两个数组和她们的大小,还有一动态开辟的内存,求交集,把交集放到动态内存dongtai,而且返回交集个数
long jiaoji(long* a[],long b[],long* alength,long blength,long* dongtai[])
2.单连表的建立,把'a'--'z'26个字母插入到连表中,而且倒叙,还要打印!
方法1:
typedef struct val
{ int date_1;
struct val *next;
}*p;
void main(void)
{ char c;
for(c=122;c>=97;c--)
{ p.date=c;
p=p->next;
}
p.next=NULL;
}
}
方法2:
node *p = NULL;
node *q = NULL;
node *head = (node*)malloc(sizeof(node));
head->data = ' ';head->next=NULL;
node *first = (node*)malloc(sizeof(node));
first->data = 'a';first->next=NULL;head->next = first;
p = first;
int longth = 'z' - 'b';
int i=0;
while ( i<=longth )
{
node *temp = (node*)malloc(sizeof(node));
temp->data = 'b'+i;temp->next=NULL;q=temp;
head->next = temp; temp->next=p;p=q;
i++;
}
print(head);
3.可怕的题目终于来了
象搜索的输入信息是一个字符串,统计300万输入信息中的最热门的前十条,我们每次输入的一个字符串为不超过255byte,内存使用只有1G,
请描述思想,写出算发(c语言),空间和时间复杂度,
4.国内的一些帖吧,如baidu,有几十万个主题,假设每一个主题都有上亿的跟帖子,怎么样设计这个系统速度最好,请描述思想,写出算发(c语言),空间和时间复杂度,
#include string.h
main(void)
{ char *src="hello,world";
char *dest=NULL;
dest=(char *)malloc(strlen(src));
int len=strlen(str);
char *d=dest;
char *s=src[len];
while(len--!=0)
d++=s--;
printf("%s",dest);
}
找出错误!!
#include "string.h"
#include "stdio.h"
#include "malloc.h"
main(void)
{
char *src="hello,world";
char *dest=NULL;
dest=(char *)malloc(sizeof(char)*(strlen(src)+1));
int len=strlen(src);
char *d=dest;
char *s=src+len-1;
while(len--!=0)
*d++=*s--;
*d='\0';
printf("%s",dest);
}
1. 简述一个Linux驱动程序的主要流程与功能。
2. 请列举一个软件中时间换空间或者空间换时间的例子。
void swap(int a,int b)
{
int c; c=a;a=b;b=a;
}
--->空优
void swap(int a,int b)
{
a=a+b;b=a-b;a=a-b;
}
6. 请问一下程序将输出什么结果?
char *RetMenory(void)
{
char p[] = “hellow world”;
return p;
}
void Test(void)
{
char *str = NULL;
str = RetMemory();
printf(str);
}
RetMenory执行完毕,p资源被回收,指向未知地址。返回地址,str的内容应是不可预测的, 打印的应该是str的地址
写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss"的首地址传给intputstr后,函数将返回
9,outputstr所指的值为
int continumax(char *outputstr, char *inputstr)
{
char *in = inputstr, *out = outputstr, *temp, *final;
int count = 0, maxlen = 0;
while( *in != '\0' )
{
if( *in > 47 && *in < 58 )
{
for(temp = in; *in > 47 && *in < 58 ; in++ )
count++;
}
else
in++;
if( maxlen < count )
{
maxlen = count;
count = 0;
final = temp;
}
}
for(int i = 0; i < maxlen; i++)
{
*out = *final;
out++;
final++;
}
*out = '\0';
return maxlen;
}
不用库函数,用C语言实现将一整型数字转化为字符串
方法1:
int getlen(char *s){
int n;
for(n = 0; *s != '\0'; s++)
n++;
return n;
}
void reverse(char s[])
{
int c,i,j;
for(i = 0,j = getlen(s) - 1; i < j; i++,j--){
c = s[ i];
s[ i] = s[j];
s[j] = c;
}
}
void itoa(int n,char s[])
{
int i,sign;
if((sign = n) < 0)
n = -n;
i = 0;
do{/*以反序生成数字*/
s[i++] = n%10 + '0';/*get next number*/
}while((n /= 10) > 0);/*delete the number*/
if(sign < 0)
s[i++] = '-';
s[ i] = '\0';
reverse(s);
}
方法2:
#include <iostream>
using namespace std;
void itochar(int num);
void itochar(int num)
{
int i = 0;
int j ;
char stra[10];
char strb[10];
while ( num )
{
stra[i++]=num%10+48;
num=num/10;
}
stra[ i] = '\0';
for( j=0; j < i; j++)
{
strb[j] = stra[i-j-1];
}
strb[j] = '\0';
cout<<strb<<endl;
}
int main()
{
int num;
cin>>num;
itochar(num);
return 0;
}
前几天面试,有一题想不明白,请教大家!
typedef struct
{
int a:2;
int b:2;
int c:1;
}test;
test t;
t.a = 1;
t.b = 3;
t.c = 1;
printf("%d",t.a);
printf("%d",t.b);
printf("%d",t.c);
谢谢!
t.a为01,输出就是1
t.b为11,输出就是-1
t.c为1,输出也是-1
3个都是有符号数int嘛。
这是位扩展问题
01
11
1
编译器进行符号扩展
求组合数: 求n个数(1....n)中k个数的组合....
如:combination(5,3)
要求输出:543,542,541,532,531,521,432,431,421,321,
#include<stdio.h>
#include <memory.h>
int pop(int *);
int push(int );
void combination(int ,int );
int *stack;
int top;
int k;
int main()
{
int n,m;
printf("Input two numbers:\n");
while( (2!=scanf("%d%*c%d",&n,&m)) )
{
fflush(stdin);
printf("Input error! Again:\n");
}
k=m;
top=-1;
stack = new int[n];
memset(stack, 0, sizeof(int)*n);
combination(n,m);
printf("\n");
delete[] stack;
}
void combination(int m,int n)
{
int temp=m;
push(temp);
while(1)
{
if(1==temp)
{
if(pop(&temp)&&stack[0]==n) //µ±Õ»µ×ÔªËØµ¯³ö&&Ϊ¿ÉÄÜÈ¡µÄ×îСֵ£¬Ñ»·Í˳ö
break;
}
else if( push(--temp))
{
for(int i=0;i<n;i++)
printf("%d",stack[ i]);//¡ìä¡§¨¬¡è@?
printf(" ");
pop(&temp);
}
}
}
int push(int i)
{
stack[++top]=i;
if(top<k-1)
return 0;
else
return 1;
}
int pop(int *i)
{
*i=stack[top--];
if(top>=0)
return 0;
else
return 1;
}
1、用指针的方法,将字符串“ABCD1234efgh”前后对调显示
#include <stdio.h>
#include <string.h>
#include <dos.h>
int main()
{
char str[] = "ABCD1234efgh";
int length = strlen(str);
char * p1 = str;
char * p2 = str + length - 1;
while(p1 < p2)
{
char c = *p1;
*p1 = *p2;
*p2 = c;
++p1;
--p2;
}
printf("str now is %s\n",str);
system("pause");
return 0;
}
2、有一分数序列:1/2,1/4,1/6,1/8……,用函数调用的方法,求此数列前20项的和
#include <stdio.h>
double getValue()
{
double result = 0;
int i = 2;
while(i < 42)
{
result += 1.0 / i;//一定要使用1.0做除数,不能用1,否则结果将自动转化成整数,即0.000000
i += 2;
}
return result;
}
int main()
{
printf("result is %f\n", getValue());
system("pause");
return 0;
}
有一个数组a[1000]存放0--1000;要求每隔二个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。
以7个数为例:
{0,1,2,3,4,5,6,7} 0-->1-->2(删除)-->3-->4-->5(删除)-->6-->7-->0(删除),如此循环直到最后一个数被删除。
方法1:数组
#include <iostream>
using namespace std;
#define null 1000
int main()
{
int arr[1000];
for (int i=0;i<1000;++i)
arr[ i]=i;
int j=0;
int count=0;
while(count<999)
{
while(arr[j%1000]==null)
j=(++j)%1000;
j=(++j)%1000;
while(arr[j%1000]==null)
j=(++j)%1000;
j=(++j)%1000;
while(arr[j%1000]==null)
j=(++j)%1000;
arr[j]=null;
++count;
}
while(arr[j]==null)
j=(++j)%1000;
cout<<j<<endl;
return 0;
}方法2:链表
#include<iostream>
using namespace std;
#define null 0
struct node
{
int data;
node* next;
};
int main()
{
node* head=new node;
head->data=0;
head->next=null;
node* p=head;
for(int i=1;i<1000;i++)
{
node* tmp=new node;
tmp->data=i;
tmp->next=null;
head->next=tmp;
head=head->next;
}
head->next=p;
while(p!=p->next)
{
node *q=p->next->next;
p->next->next=p->next->next->next;
delete q;
p=p->next->next;
}
cout<<p->data;
delete p;
return 0;
}
方法3:通用算法
#include <stdio.h>
#define MAXLINE 1000 //元素个数
/*
MAXLINE 元素个数
a[] 元素数组
R[] 指针场
suffix 下标
index 返回最后的下标序号
values 返回最后的下标对应的值
start 从第几个开始
K 间隔
*/
int find_n(int a[],int R[],int K,int& index,int& values,int s=0) {
int suffix;
int front_node,current_node;
suffix=0;
if(s==0) {
current_node=0;
front_node=MAXLINE-1;
}
else {
current_node=s;
front_node=s-1;
}
while(R[front_node]!=front_node) {
printf("%d\n",a[current_node]);
R[front_node]=R[current_node];
if(K==1) {
current_node=R[front_node];
continue;
}
for(int i=0;i<K;i++){
front_node=R[front_node];
}
current_node=R[front_node];
}
index=front_node;
values=a[front_node];
return 0;
}
int main(void) {
int a[MAXLINE],R[MAXLINE],suffix,index,values,start,i,K;
suffix=index=values=start=0;
K=2;
for(i=0;i<MAXLINE;i++) {
a[ i]=i;
R[ i]=i+1;
}
R[i-1]=0;
find_n(a,R,K,index,values,2);
printf("the value is %d,%d\n",index,values);
return 0;
}
试题:
void test2()
{
char string[10], str1[10];
int i;
for(i=0; i<10; i++)
{
str1[ i] = 'a';
}
strcpy( string, str1 );
}
解答:对试题2,如果面试者指出字符数组str1不能在数组内结束能够给3分;如果面试者指出strcpy(string, str1)调用使得从str1内存起复制到string内存起所复制的字节数具有不确定性能够给7分,在此基础上指出库函数strcpy工作方式的给10分;
str1不能在数组内结束:因为str1的存储为:{a,a,a,a,a,a,a,a,a,a},没有'\0'(字符串结束符),因此不能结束
strcpy( char *s1,char *s2)她的工作原理是,扫描s2指向的内存,逐个字符付到s1所指向的内存,直到碰到'\0',因为str1结尾没有'\0',因此具有不确定性,不知道她后面还会付什么东东。
正确应如下
void test2()
{
char string[10], str1[10];
int i;
for(i=0; i<9; i++)
{
str1[ i] = 'a'+i; //把abcdefghi赋值给字符数组
}
str[i ]='\0';//加上结束符
strcpy( string, str1 );
}
第二个code题是实现strcmp
int StrCmp(const char *str1, const char *str2)
做是做对了,没有抄搞,比较乱
int StrCmp(const char *str1, const char *str2)
{
assert(str1 && srt2);
while (*str1 && *str2 && *str1 == *str2) {
str1++, str2++;
}
if (*str1 && *str2)
return (*str1-*str2);
elseif (*str1 && *str2==0)
return 1;
elseif (*str1 = = 0 && *str2)
return -1;
else
return 0;
}
int StrCmp(const char *str1, const char *str2)
{
//省略判断空指针(自己保证)
while(*str1 && *str1++ = = *str2++);
return *str1-*str2;
}
第三个code题是实现子串定位
int FindSubStr(const char *MainStr, const char *SubStr)
做是做对了,没有抄搞,比较乱
int MyStrstr(const char* MainStr, const char* SubStr)
{
const char *p;
const char *q;
const char * u = MainStr;
//assert((MainStr!=NULL)&&( SubStr!=NULL));//用断言对输入进行判断
while(*MainStr) //内部进行递增
{
p = MainStr;
q = SubStr;
while(*q && *p && *p++ == *q++);
if(!*q )
{
return MainStr - u +1 ;//MainStr指向当前起始位,u指向
}
MainStr ++;
}
return -1;
}
分析:
int arr[] = {6,7,8,9,10};
int *ptr = arr;
*(ptr++)+=123;
printf(“ %d %d ”, *ptr, *(++ptr));
输出:8 8
过程:对于*(ptr++)+=123;先做加法6+123,然后++,指针指向7;对于printf(“ %d %d ”, *ptr, *(++ptr));从后往前执行,指针先++,指向8,然后输出8,紧接着再输出8
华为全套完整试题
高级题
6、已知一个单向链表的头,请写出删除其某一个结点的算法,要求,先找到此结点,然后删除。
slnodetype *Delete(slnodetype *Head,int key){}中if(Head->number==key)
slnodetype *Delete(slnodetype *Head,int key)
{
slnodetype *pre, *cur;
if(!Head)
return Head;
cur = Head;
if(Head->number==key)
{
Head=Head->next;
delete cur;
return Head;
}
pre = cur;
cur == cur->next;
while(cur)
{
if(cur->number == key)
{
pre->next = cur->next;
delete cur;
return pre;
}
cur = cur->next;
}
return null;
}
有一个16位的整数,每4位为一个数,写函数求她们的和。
解释:
整数0111
和 1101+0101+1011+0111
感觉应该不难,当时对题理解的不是很清楚,因此写了一个函数,也不知道对不对。
疑问:
既然是16位的整数,0111是2进制的,那么函数参数怎么定义呢,请大虾指教。
答案:用十进制做参数,计算时按二进制考虑。
/* n就是16位的数,函数返回它的四个部分之和 */
char SumOfQuaters(unsigned short n)
{
char c = 0;
int i =
展开阅读全文