有几道笔试题记录一下:
1.TCP协议属于OSI 7层模型中的(4)层
TCP协议属于传输层,在OSI的第四层
2.typedef struct
{
char flag[3];
short value;
}SampleStruct;
typedef union
{
char flag[3];
short value;
}SampleUnion;
求sizeof(SampleStruct)和sizeof(SampleUnion)
写了一段程序测试一下,用的是DEV C++编译器,32位windows xp系统:
#include <stdio.h>
typedef struct
{
char flag[3];
short value; //
}SampleStruct;
typedef union
{
char flag[3];
short value; //
}SampleUnion;
int main()
{
SampleStruct a;
SampleUnion b;
printf("%d, %d, %d\n", sizeof(char), sizeof(short), sizeof(a.flag));
printf("%d, %d\n", sizeof(a), sizeof(b));
return 0;
}
返回的结果为:
1, 2, 3
6, 4
将struct和union中的short value注释掉后,返回的结果为:
1, 2, 3
3, 3
3.输入baseStr是一个字符串,请将其中所有连续出现的多个空格都替换成一个空格,单一空格需保留,返回值是替换后的字符串的长度。如果使用任何库函数必须同时给出库函数的实现。
public static int RemoveMultipleSpaces(char[] baseStr)
#include <stdio.h>
//#include <string.h>
#define MAX 1000
int strlen(const char *str)
{
int count=0;
const char *p=str;
if(str==NULL || *str=='\0')
return 0;
while(*p!='\0')
{
++p;
++count;
}
return count;
}
int RemoveMultipleSpaces(char baseStr[])
{
char *p=baseStr, *q=NULL, *r=NULL;
if(strlen(baseStr)==0)
return 0;
while(*p!='\0')
{
if(*p==' ' && *(p+1)==' ')
{
q=p+1;
r=q;
while(*q==' ')
q++;
if(*q=='\0')
{
*(p+1)='\0';
return strlen(baseStr);
}
else
{
while(*q!='\0')
//*r++=*q++;
{
*r=*q;
*q='\0'; // clear after copy
r++;
q++;
}
}
p++;
}
else
p++;
}
return strlen(baseStr);
}
int main()
{
int count=0;
char s[MAX];
printf("Please input a stirng:");
//while(scanf("%s",s)!=EOF)
//;
gets(s);
count=RemoveMultipleSpaces(s);
//count=RemoveMultipleSpaces("Hello "); WRONG
//count=RemoveMultipleSpaces(s);
//char s[]="Welcome to China! "; RIGHT
//count=RemoveMultipleSpaces(s);
printf("The length of the string after removing spaces is:%d\n",count);
printf("The string after modification is:%s",s);
return 0;
}

有三个地方需要注意:1,gets()函数的运用,在遇到换行符的时候输入结束,而scanf()函数在遇到空格的时候就结束了。2,在main()函数中给RemoveMultipleSpaces()传入参数的时候,注意字符串常量是不可被修改的,如上述代码中的WRONG注释。3,在去除空格函数的内部,拷贝赋值之后,被移位的部分需要置为结束符'\0',否则会出错。
4.存在整型数组result,长度为n并且满足0<=low<=mid<high<n,result[low,mid]和result[mid+1,high]均已按升序排序,完成函数将result[low,mid]和result[mid+1,high]合并升序数组。
int *buffer = new int[n];
void merge(int *result, int low, int mid, int high)
未排序数组unsort[0,1,...,n-1](n>1)排序,请使用以上merge函数完成unsort的归并排序。
#include <iostream>
using namespace std;
int Merge(int *result, int low, int mid, int high)
{
int n=high-low+1;
int *buffer=new int[n];
int begin1=low, begin2=mid+1, end1=mid, end2=high, k=0;
while((begin1<=end1) && (begin2<=end2))
{
if(result[begin1]<result[begin2])
buffer[k++]=result[begin1++];
else
buffer[k++]=result[begin2++];
}
while(begin1<=end1)
buffer[k++]=result[begin1++];
while(begin2<=end2)
buffer[k++]=result[begin2++];
int i;
for(i=0;i<k;i++)
result[low++]=buffer[i];
delete[] buffer;
return 0;
}
int MSort(int a[], int l, int r)
{
int m=(l+r)/2;
//if(l<m) WRONG
//if(l<=m) 无限循环
if(l<r)
{
MSort(a, l, m);
MSort(a, m+1, r);
Merge(a, l, m, r);
}
return 0;
}
int MergeSort(int unsort[], int size)
{
int left=0, right=size-1;
MSort(unsort, left ,right);
return 0;
}
int main()
{
int array[]={10,9,8,7,6,5,4,3,2,1};
//int i, n=sizeof(array);
int i;
int n=sizeof(array)/sizeof(int);
MergeSort(array, n);
for(i=0;i<n;i++)
printf("%d\n",array[i]);
return 0;
}

其实就是需要写出“归并排序”的算法,题目中还简单一些,提供了一个渐进的思路。需要注意的问题是归并排序算法与Merge()函数之间的关系,需要怎样调用Merge()函数。
5.字符串A和字符串B,请将A中在B中存在的字符删除,比如A="This is the world",B="abcdefghi",则返回A为"Ts s t worl"
#include <stdio.h>
#define MAX 1000
int Modify(char *des, const char *src)
{
char *p=des, *r=NULL;
const char *q=src;
int flag=0; // 设置移位标志
while(*p!='\0')
{
while(*q!='\0')
{
if(*p==*q)
{
r=p+1;
while(*r!='\0')
{
*(r-1)=*r;
r++;
}
*(r-1)='\0';
flag=1;
break;
}
q++;
}
q=src; // reset to the original place
if(flag==0)
p++;
else
flag=0;
}
}
int main()
{
char a[MAX], b[MAX];
printf("Please input two strings:\n");
gets(a);
gets(b);
Modify(a, b);
printf("The string after modification is:%s", a);
return 0;
}

需要注意的问题,一是在修改字符串移位后原有的指针已经经过了改动不需要继续移位,二是寻找匹配的源字符串中的指针在一次遍历后需要重新归位。
1384

被折叠的 条评论
为什么被折叠?



